我收到错误消息:"SQLiteCantOpenDatabaseException: unable to open database file (code 14)" on the line: if (cursor != null && cursor.moveToFirst()) {
和
data = dbHelper.getTblQuoteNotify("tblQuoteNotify", nCounter);
我有<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
这似乎是我研究中对此问题最常见的解决方法 - 其他任何人都可以查看下面的代码,让我知道在这种情况下我可能做错了什么?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
doTimerTask();
setContentView(R.layout.activity_main);
data_textview = (TextView) findViewById(R.id.data);
timer = (TextView)findViewById(R.id.timer);
dbHelper = new DBHelper(this);
try {
dbHelper.CopyDataBaseFromAsset();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dbHelper.openDataBase();
data = new Data();
}
public void doTimerTask(){
mTimerTask = new TimerTask() {
public void run() {
handler.post(new Runnable() {
public void run() {
nCounter++;
// update TextView
timer.setText("Notify: " + nCounter);
data = dbHelper.getTblQuoteNotify("tblQuoteNotify", nCounter);
if (data != null) {
data_textview.setText(data.tblQuoteNotify);
}
05-26 12:01:28.533: E/AndroidRuntime(13445): FATAL EXCEPTION: main
05-26 12:01:28.533: E/AndroidRuntime(13445): Process: com.example.app, PID: 13445
05-26 12:01:28.533: E/AndroidRuntime(13445): android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file (code 14)
05-26 12:01:28.533: E/AndroidRuntime(13445): at android.database.sqlite.SQLiteConnection.nativeExecuteForCursorWindow(Native Method)
05-26 12:01:28.533: E/AndroidRuntime(13445): at android.database.sqlite.SQLiteConnection.executeForCursorWindow(SQLiteConnection.java:845)
05-26 12:01:28.533: E/AndroidRuntime(13445): at android.database.sqlite.SQLiteSession.executeForCursorWindow(SQLiteSession.java:836)
05-26 12:01:28.533: E/AndroidRuntime(13445): at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:62)
05-26 12:01:28.533: E/AndroidRuntime(13445): at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:144)
05-26 12:01:28.533: E/AndroidRuntime(13445): at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:133)
05-26 12:01:28.533: E/AndroidRuntime(13445): at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:197)
05-26 12:01:28.533: E/AndroidRuntime(13445): at android.database.AbstractCursor.moveToFirst(AbstractCursor.java:237)
05-26 12:01:28.533: E/AndroidRuntime(13445): at com.example.app.data.DBHelper.getTblQuoteNotify(DBHelper.java:67)
05-26 12:01:28.533: E/AndroidRuntime(13445): at com.example.app.MainActivity$1$1.run(MainActivity.java:69)
答案 0 :(得分:1)
通过降低TimerTask的频率解决了这个问题。尝试过快地访问数据库会导致此错误。
答案 1 :(得分:0)
在打开数据库之前,您正在调用doTimerTask();
。首先打开数据库,然后在审核代码时调用doTimerTask(); method
,我无法运行您的代码,但它似乎是正确的,但我会建议:
.db
,.sqlite
等像这样更改你的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//doTimerTask();
setContentView(R.layout.activity_main);
data_textview = (TextView) findViewById(R.id.data);
timer = (TextView)findViewById(R.id.timer);
dbHelper = new DBHelper(this);
try {
dbHelper.CopyDataBaseFromAsset();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dbHelper.openDataBase();
data = new Data();
doTimerTask(); //**** call doTimerTask here
}
我不确定它会不会帮助你,因为你还没有发布代码,我们可以通过它修复bug,无论如何我建议你访问this和this
看看你的DBpath,它是否正确?正确放入包裹名称。
public final static String DATABASE_PATH ="/data/data/you.package.name/databases/";
或检查光标是否正常工作,查询是否正确?