我已经为我的Android应用程序创建了一个包含静态数据的数据库,并且不需要更新/删除功能,因此当应用程序启动时,我想检查数据库是否存在,如果不存在则执行我的dbAdapter类.我知道它是一个简单的if语句,但我只是想知道查询db是否存在的最有效方法.
干杯
rds*_*rds 163
我宁愿直接检查文件的存在:
private static boolean doesDatabaseExist(Context context, String dbName) {
File dbFile = context.getDatabasePath(dbName);
return dbFile.exists();
}
Run Code Online (Sandbox Code Playgroud)
Mat*_*adt 79
/**
* Check if the database exist and can be read.
*
* @return true if it exists and can be read, false if it doesn't
*/
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
checkDB = SQLiteDatabase.openDatabase(DB_FULL_PATH, null,
SQLiteDatabase.OPEN_READONLY);
checkDB.close();
} catch (SQLiteException e) {
// database doesn't exist yet.
}
return checkDB != null;
}
Run Code Online (Sandbox Code Playgroud)
其中DB_FULL_PATH是数据库文件的路径.
我不只是检查文件是否存在的原因是因为它不会告诉(a)它是否是一个sqlite db文件,(b)文件没有损坏并且实际上可以被读取,即由于部分下载或者它已经被创造了.
| 归档时间: |
|
| 查看次数: |
80222 次 |
| 最近记录: |