我已经为我的Android应用程序创建了一个数据库,该数据库包含静态数据,并且不需要更新/删除功能,因此在应用程序启动时,我想检查数据库是否存在,如果不存在,请执行dbAdapter类。我知道它是一个简单的if语句,但我只是想知道查询数据库是否存在的最有效方法。
干杯
Answers:
/**
* 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;
}
其中DB_FULL_PATH是数据库文件的路径。
我之所以不只是检查文件是否存在,是因为它无法告诉(a)它是否是sqlite db文件,(b)该文件是否未损坏并且可以实际读取,即由于部分下载或然而已经被创造了。
checkDB = SQLiteDatabase.openDatabase(DB_FULL_PATH, null, SQLiteDatabase.OPEN_READONLY); if (checkDB == null) {// database doesn't exist yet.}
。我想这不是你的错,看来Java困扰着这种废话……
我宁愿直接检查文件的存在:
private static boolean doesDatabaseExist(Context context, String dbName) {
File dbFile = context.getDatabasePath(dbName);
return dbFile.exists();
}
当您初始化以下类时:
mOpenHelper = new DatabaseHelper(getContext());
如果数据库不存在,它将自动创建数据库。它还允许您通过将DB_VER更改为更大的数字来升级数据库。
然后,您便可以查询数据库的使用情况:
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
上面的方法为您提供了db.query()和db.insert()等方法。
private static class DatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "db_name.db";
private static final int DB_VER = 1;
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VER);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE table_name (" + "_id INTEGER PRIMARY KEY, "
+ " column_name_2 TEXT );");
.execSQL("INSERT INTO table_name "
+ "(column_name_2) "
+ "VALUES " + "('hello world');");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG + "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
try {
db.execSQL("DROP TABLE IF EXISTS table_name");
onCreate(db);
} catch (SQLException e) {
Log.e(TAG + "getting exception "
+ e.getLocalizedMessage().toString());
}
}
}
很简单:在try块中使用da databse路径打开数据库,如下所示:
try{
SQLiteDatabase dbe = SQLiteDatabase.openDatabase("/data/data/bangla.rana.fahim/databases/dictionary", null,0);
Log.d("opendb","EXIST");
dbe.close();
}
如果发生异常,则数据库不会退出,因此请创建它:
catch(SQLiteException e){
Log.d("opendb","NOT EXIST");
SQLiteDatabase db = openOrCreateDatabase("dictionary", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS LIST(wlist varchar);");
db.execSQL("INSERT INTO LIST VALUES('খবর');");
db.execSQL("INSERT INTO LIST VALUES('কবর');"); //whatever you want
db.close();
}
就是这样,您完成了:)
我尝试了Mathias Conradt提供的版本,但发现仅检查DB!= null是否不足。我对此进行了修改:
/**
* 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(String InDBFile) {
SQLiteDatabase checkDB = null;
boolean res = false;
try {
checkDB = SQLiteDatabase.openDatabase(InDBFile, null,
SQLiteDatabase.OPEN_READONLY);
res = (checkDB.getVersion() > 0);
checkDB.close();
} catch (SQLiteException e) {
// database doesn't exist yet.
Log.e("checkDataBase", "Selected file could not be opened as DB.");
res = false;
}
return res;
}
我找到了一个更简单的解决方案:
context.databaseList().contains("table_name")