我将数据库存储在我的资产文件夹中,并在运行时复制过来。我现在有一个简单的活动,可以进行一个简单的数据库调用:
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被称为signalingonopen以复制数据库时设置一个标志。显然这两个都有点老套,我真的很好奇发生了什么。
我计划将实际的数据库调用移动到活动之外的单独帮助程序类,我只是直接调用数据库来测试它。
最佳答案
如果要在第一次运行时从资产复制数据库,并从data/data/program.name/database打开数据库,则应使用如下代码:
public class YourDatabase extends SQLiteOpenHelper
public YourDatabase(Context context) {
super(context, DatabaseName, null, DATABASE_VERSION);
this.dbContext = context;
// checking database and open it if exists
if (checkDataBase()) {
openDataBase();
} else {
try {
this.getReadableDatabase();
copyDataBase();
this.close();
openDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
Toast.makeText(context, "Initial database is created", Toast.LENGTH_LONG).show();
}
}
完整答案可从this问题中阅读