我不想使用我在计算机上创建的数据库文件。
虽然this正在运行,但我认为这是一个糟糕的解决方法,因为它不会使用现有的API,而是创建自己的api。
我希望能够使用getWritableDataBase(),onCreate(SQLiteDatabase db)和onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)方法。
所以,这就是我所做的,而且由于某些原因它不起作用。我认为,如果我重写现有的数据库,它会工作,但在查询时我得到的表不存在异常。
public void onCreate(SQLiteDatabase db) {
File f = new File(db.getPath());
try {
InputStream is = context.getResources().getAssets().open("words1.db");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
FileOutputStream fos = new FileOutputStream(f);
fos.write(buffer);
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
那么我怎样才能使用预先填充的数据库,同时使用现有的api?
答案 0 :(得分:1)
使用您的应用程序预先打包数据库的最简单方法是使用Jeff Gilfelt's SQLiteAssetHelper
。虽然它确实需要您为项目添加一个小JAR,但他的代码已经过测试和调试,而且您只需要很少的额外代码。
Here is a sample project演示使用SQLiteAssetHelper
。