Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
728 views
in Technique[技术] by (71.8m points)

sqlite - Unable to open database in Android Pie (Android 9)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...