Raven Choi
Raven Choi

Reputation: 83

Android SQLITE, unable to open database in Marshmallow only

I built an app. (The app can save favourite items using SQLite Database). I tried running it on Android Nougat and there is no error. When I tried running it on Android Marshmallow, the app won't open (Force close) and when I check the error it says unable to open database. After that, I tried running it on Android Lollipop and there is no error found.

I trace the error and found on the Marshmallow, the database is not created. (I used DB debugger to check the DB). I already add these user permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

and nothing happened. Anyone else knows about this??

**NEW EDIT
this is the syntax to check if the db already created or not

public DataHeper(Context context) {
        // TODO Auto-generated constructor stub
        this.mContext = context;
        databasePath = "data/data/"+context.getPackageName()+"/databases/data.sqlite";
        databaseFile = new File(databasePath);
        if(!databaseFile.exists())
            try {
                deployDataBase(databaseName, databasePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
    }



    public int getTotalQuotesNoFilter(){
            int i =0;
            String query="";
            Cursor  cursor;
            //This is the line that produce error (On Marshmallow only).
            //I trace it and found out the db not created, thats why opendatabase produce error
            database = SQLiteDatabase.openDatabase(databasePath, null, SQLiteDatabase.OPEN_READWRITE);
                query = "SELECT count(quotes._id) FROM quotes;";
                WriteLog.d("ThangTB", "query: "+query);
                try {
                    cursor =  database.rawQuery(query, null);   
                    if (cursor != null){
                        boolean bool = cursor.moveToFirst();
                        int j = cursor.getInt(0);
                        i = j;
                     }

                    if (cursor!=null) {
                        cursor.close();
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }finally{
                    database.close();
                }

            return i;

        }

Upvotes: 2

Views: 2571

Answers (3)

Sanjeev
Sanjeev

Reputation: 151

SQLite Db needs no user permission. So I guess there is some other issue. Post your code.

Upvotes: 2

Naman Bajaj
Naman Bajaj

Reputation: 43

From Marshmallow you need to request permission from user at run time, I also had the same problem check this link, it will help you. https://www.androidhive.info/2016/11/android-working-marshmallow-m-runtime-permissions/

Upvotes: 0

Rishabh Dugar
Rishabh Dugar

Reputation: 616

From Android M onwards, permissions needs to obtained from User while he/she is using the app, solely mentioning in Manifest wont do.

It is super easy to implement it using this library.

https://github.com/googlesamples/easypermissions

Upvotes: 1

Related Questions