【发布时间】:2015-12-24 07:45:33
【问题描述】:
当我将 android 版本和路径从 externalized 更改为默认数据库文件夹时,我不知道错误来自哪里。 我发现原因可能是访问日志文件的问题被锁定(它是由程序创建的)。
“测试”正在读取数据,如果存在则更新,如果不存在则插入。这是在从文件中读取行时循环完成的。出于测试目的,我对其进行了简化,但错误是相同的。
package ...;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class test {
public test(SQLiteDatabase MyDB)
{
MyDB.execSQL("CREATE TABLE IF NOT EXISTS testtable (mytext TEXT, number INT PRIMARY KEY)", new String[] {});
for (int i = 1; i < 5; i++)
{
String[] ColArray = { "any text", String.valueOf(i) };
Cursor readCursor = MyDB.rawQuery("SELECT mytext FROM testtable WHERE number=?", new String[] { String.valueOf(i) });
if (!readCursor.moveToNext()) // Error when executing moveToNext
/*
In the 2nd time it runs over this point an Error occurs:
android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file (code 14)
I need to remove the journal file to be able to connect to the database it again - the change made by the Insert/Update is successfully saved!
*/ {
readCursor.close();
MyDB.execSQL("INSERT INTO testtable (mytext, number) VALUES(?, ?)", ColArray);
}
else {
readCursor.close();
MyDB.execSQL("UPDATE testtable SET mytext=? WHERE number=?", ColArray);
}
}
}
}
MyDB 是一个有效的 DB-Connection。 1)创建表 2) 表已读,但未找到第 1 行 2a) 如果读取已关闭,则到现在为止再次读取是可能的。 3) 第 1 行已插入 4) 再次尝试 READ 时(也找不到第 2 行),移动光标失败。
需要手动删除日志文件,否则在尝试连接时应用将无法重新启动。
请帮忙!谢谢。
【问题讨论】:
-
在 API9 (2.3.4) 设备上测试,相同的代码可以正常工作,没有任何错误。有错误的设备有一个android版本API16(4.1.2)
标签: android sqlite select insert journal