在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