无法在Android Pie(Android 9)中打开数据库 [英] Unable to open database in Android Pie (Android 9)
问题描述
我使用checkDataBase函数来确保数据库是否已经存在,以避免每次您在Oreo中打开应用程序时都重新复制文件。
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;
}
出现此错误:os_unix.c:36667 :(2)打开(/data/data/my.androidPieTrial.app/databases/admin.db)
android.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(代码14 SQLITE_CANTOPEN):无法打开数据库
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
推荐答案
我得到了解决方案。在Android Oreo及以下版本中,我访问db的方式运行良好,但在Android Pie中却无法使用。这是在Android Pie中处理它的方式。
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();
这篇关于无法在Android Pie(Android 9)中打开数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!