【发布时间】:2010-08-02 09:57:41
【问题描述】:
我为我的 android 应用程序创建了一个数据库,其中包含静态数据并且不需要更新/删除功能,因此当应用程序启动时,我想检查数据库是否存在,如果不存在则执行我的 dbAdapter 类。我知道它是一个简单的 if 语句,但我只是想知道查询数据库是否存在的最有效方法。
干杯
【问题讨论】:
我为我的 android 应用程序创建了一个数据库,其中包含静态数据并且不需要更新/删除功能,因此当应用程序启动时,我想检查数据库是否存在,如果不存在则执行我的 dbAdapter 类。我知道它是一个简单的 if 语句,但我只是想知道查询数据库是否存在的最有效方法。
干杯
【问题讨论】:
我宁愿直接检查文件是否存在:
private static boolean doesDatabaseExist(Context context, String dbName) {
File dbFile = context.getDatabasePath(dbName);
return dbFile.exists();
}
【讨论】:
/**
* 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被这种废话所困扰......
当你初始化下面的类时:
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());
}
}
}
【讨论】:
这很简单: 使用 da 数据库的路径在 try 块中打开您的数据库,例如:
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;
}
【讨论】:
在您的主要活动中创建一个全局数据库助手类对象。在 MainActivity 的 onCreate() 函数中试试这个:
//global database helper variable
DatabaseHelper dbh;
//in the onCreate()
if(dbh.getReadableDatabase()!=null)
//view the list
else
//create db
【讨论】:
我找到了一个更简单的解决方案:
context.databaseList().contains("table_name")
【讨论】: