【发布时间】:2012-10-09 08:56:31
【问题描述】:
我已按照 here 的说明将现有 SQLite 数据库引入您的 Android 应用。
当我查询表“android_metadata”时,这很好。但是,当我对自己的表“words”(主整数键为 _id)运行类似查询时,我得到一个表不存在异常并且应用程序崩溃。
这是为什么呢?
代码:
Cursor c = myDatabase.query("android_metadata", null, null, null, null, null, null, null);
有效但
Cursor c = myDatabase.query("words", null, null, null, null, null, null, null);
返回表不存在异常。
这就是我创建数据库的方式(对路径和文件名的引用是正确的):
private void copyDatabase() throws IOException
{
//Open local db as the input stream
InputStream myInput = mContext.getAssets().open(DB_NAME);
//Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//Transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length = 0;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
(注意:在我看来,表格就在那里。我正在我的 SQLite 浏览器中查看它。)
【问题讨论】:
-
没有一些代码没有人能够回答......
-
这不是编码问题 - 如果我能找到一个表而不是另一个表,那么这是如何编辑 sqlite 数据库以适应 android 的问题。我敢肯定有人遇到过这个......
-
有关更多代码,请参阅随附的“此处”链接。这是非常标准的 SQLiteDatabase 帮助程序。
-
您的数据库的路径是否遵循以下规则:/data/data/your.applications.package/databases Android 期望您的数据库位于该位置。 stackoverflow.com/questions/2318784/the-sqlite-database-path
-
是的。就像我说的,我可以查询“android_metadata”表,但是当我查询自己的“words”表时,我得到了一个异常。