在Android中,我使用以下方法來查看sqlite數據庫是否存在,以及是否可以打開並使用它。在Android中,檢查sqlite數據庫是否存在失敗
如果測試失敗,我從資產中複製數據庫文件(這應該只發生一次,當用戶第一次啓動應用程序時)。
/*
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
} catch(SQLiteException e) {
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
的問題是,我收到來自用戶的報告說他們的數據已經被消滅和調查時,我可以看到數據庫被替換爲資產的數據庫。因此,即使用戶已有數據庫文件,但由於某些原因,SQLiteDatabase.openDatabase()有時會引發錯誤。我自己無法重現此問題,但似乎發生在某些用戶身上。
任何人都有一個想法可能是什麼問題在這裏?有沒有更好的方法來做這個測試?
請與本http://stackoverflow.com/questions/20728808/android-reading-stored-sqlite-database – gandharv09