6

I used checkDataBase function to ensure if the database already exist to avoid re-copying the file each time you open the application in Oreo. But in Android Pie it is not working.

private boolean checkDataBase (String dbName, int dbVersion) {

    SQLiteDatabase checkDB = null;

    try {

        String myPath = DB_PATH + dbName;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.CONFLICT_ABORT);

        if (checkDB.getVersion() < dbVersion) {
            Timber.d("Delete database called");
            myContext.deleteDatabase(dbName);
            return false;
        }
    } catch(SQLiteException e) {

    }

    if(checkDB != null){
        checkDB.close();
    }

    return checkDB != null;
}

Getting this error: os_unix.c:36667: (2) open(/data/data/my.androidPieTrial.app/databases/admin.db) android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14 SQLITE_CANTOPEN): Could not open database

Veeresh P
  • 436
  • 3
  • 15

3 Answers3

12

I got the solution. In Android Oreo and below version, the way i am accessing db works fine but in Android Pie it wasn't working.This is the way to handle it in Android Pie.

if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        MySQLiteOpenHelper helper = new MySQLiteOpenHelper();
        SQLiteDatabase database = helper.getReadableDatabase();
        myPath = database.getPath();

    } else {
        String DB_PATH = Environment.getDataDirectory() + "/data/my.trial.app/databases/";
        myPath = DB_PATH + dbName;
    }

    checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    checkDB.disableWriteAheadLogging();
Veeresh P
  • 436
  • 3
  • 15
  • i have same problem so can u please help me to sort out this issue i cant understand what u r saying in this code snippet can u please more explain – Developers Park Oct 04 '18 at 12:13
  • 1
    @APOORVADOSHI basically instead of using a hard coded path based upon the Environment.getDataDirectory for Pie an improved (get the path via the system) method is being used. However really the correct way is to use `context.getDatabasePath(the_database_name).getAbsolutePath();` (which has been available since API 1 and thus works with all versions). – MikeT Nov 03 '18 at 20:42
  • 3
    I am also facing the same issue. Can you please share the MySQLiteOpenHelper class, because otherwise, its really unclear how to fix the issue . – Faisal Nov 19 '18 at 19:48
  • 1
    this worked for me but an important part was using: checkDB.disableWriteAheadLogging(); – u2tall Aug 15 '19 at 19:07
  • I am also having same issue but its not working for me – Tanveer Ahmed Oct 23 '19 at 18:05
  • I am using SQLiteAssetHelper and getting NullPointerException when trying to open the database for the first time. The database doesn't get copied from the asset folder. – Ankit Sahu Sep 05 '22 at 07:33
4

Override following method on SQLiteOpenHelper class for android PIE OS version.

@Override
public void onOpen(SQLiteDatabase db) {
    super.onOpen(db);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        db.disableWriteAheadLogging();
    }
}
Gaurav Agrawal
  • 313
  • 4
  • 15
3
context.getDatabasePath(the_database_name).getPath();

Works fine since getDatabasePath() returns the absolute path on the filesystem where a database created. Works for all version of android