【发布时间】:2011-07-19 00:13:40
【问题描述】:
我的数据库存储在我的资产文件夹中,并在运行时复制过来。我目前有一个简单的 Activity 进行简单的数据库调用:
DBAdapter adapter = new DBAdapter(HomeActivity.this);
final SQLiteDatabase db = adapter.getReadableDatabase();
final Cursor c = db.query("exercises", new String[] { "name" }, null,
null, null, null, null);
startManagingCursor(c);
if (c.moveToFirst())
Log.e(TAG, c.getString(0));
else
Log.e(TAG,"No dice");
下面是我的 DBAdapter.java(它扩展了 open helper):
public class DBAdapter extends SQLiteOpenHelper {
private static final int DB_VERSION = 1;
private static String DB_PATH = "";
private static final String DB_NAME = "gymrat.db";
private final Context myContext;
private static final String TAG = "GymRat.DBAdapter";
/**
* Constructor Takes and keeps a reference of the passed context in order to
* access to the application assets and resources.
*
* @param context
*/
public DBAdapter(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.myContext = context;
DB_PATH = "/data/data/"
+ context.getApplicationContext().getPackageName()
+ "/databases/";
}
/**
* Copies your database from your local assets-folder to the just created
* empty database in the system folder, from where it can be accessed and
* handled. This is done by transferring bytestream.
* */
private void copyDatabase() throws IOException {
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
/**
* Call on creating data base for example for creating tables at run time
*/
@Override
public void onCreate(SQLiteDatabase db) {
Log.e(TAG, "onCreate");
try {
copyDatabase();
} catch (IOException e) {
Log.e(TAG,"IOException copying Database");
}
}
@Override
public void onOpen(SQLiteDatabase db) {
Log.e(TAG, "onOpen");
try {
db.rawQuery("select * from exercises", null);
} catch ( SQLiteException e) {
Log.e(TAG,"DB copy didn't work");
try {
copyDatabase();
} catch (IOException e1) {
Log.e(TAG,"IOException recopying DB");
}
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.e(TAG, "Upgrading");
}
}
我从here 获取了这个适配器并对其进行了修改,因为它似乎没有使用 OpenHelper 功能。
我的问题:每当我的应用在安装后或擦除应用上的数据后第一次运行时,onCreate 会按照我的预期首先被调用,然后调用 copyDatabase 方法。问题是数据库没有被复制——或者如果复制了,它会立即被覆盖。而是创建了一个只有“android meta_data”表的默认数据库,导致我的活动中的查询抛出异常,因为该表不存在。
如果我在 onOpen 方法中再次复制数据库,它工作正常。我是在做一些乱七八糟的事情还是错过了一些导致 SQLiteOpenHelper 创建默认数据库的调用?创建的默认数据库使用构造函数中指定的相同名称和版本号。
现在,如您所见,我正在使用 onOpen 中的虚拟查询来查看预期的表是否存在,如果不继续并重新复制数据库。另一种选择是在调用 onCreate 时设置一个标志,发送信号 onOpen 以复制数据库。显然这些都有点hacky,我真的很好奇发生了什么。
我计划将实际的数据库调用移动到 Activity 之外的单独帮助程序类,我只是直接调用 db 来测试它。
【问题讨论】: