数据库未关闭错误,尽快关闭代码

时间:2012-11-19 17:20:12

标签: android sql

我有一个问题,我现在正在和我一起玩耍几天。它一直给我一个错误,我的数据库没有关闭。我从我的sql db中获取项目并将它们放在带有自定义适配器的gridview中。如果单击某个项目,它将转到另一个类(answer.java)。什么都不做,按下后退按钮不会导致错误,但如果我调用某个功能并在此功能后按下后退按钮,则按下后退按钮时会出现close()错误。 所以这是确切的错误:

    11-19 19:56:22.485: E/Database(7403): close() was never explicitly called on database '/data/data/com.test.game/databases/quizDb' 
11-19 19:56:22.485: E/Database(7403): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
11-19 19:56:22.485: E/Database(7403):   at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1847)
11-19 19:56:22.485: E/Database(7403):   at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
11-19 19:56:22.485: E/Database(7403):   at com.test.game.db.DataBaseHelper.openDataBase(DataBaseHelper.java:117)
11-19 19:56:22.485: E/Database(7403):   at com.test.game.Answer.loadDataBase(Answer.java:179)
11-19 19:56:22.485: E/Database(7403):   at com.test.game.Answer.onClick(Answer.java:240)
11-19 19:56:22.485: E/Database(7403):   at android.view.View.performClick(View.java:2491)
11-19 19:56:22.485: E/Database(7403):   at android.view.View$PerformClick.run(View.java:9086)
11-19 19:56:22.485: E/Database(7403):   at android.os.Handler.handleCallback(Handler.java:587)
11-19 19:56:22.485: E/Database(7403):   at android.os.Handler.dispatchMessage(Handler.java:92)
11-19 19:56:22.485: E/Database(7403):   at android.os.Looper.loop(Looper.java:130)
11-19 19:56:22.485: E/Database(7403):   at android.app.ActivityThread.main(ActivityThread.java:3683)
11-19 19:56:22.485: E/Database(7403):   at java.lang.reflect.Method.invokeNative(Native Method)
11-19 19:56:22.485: E/Database(7403):   at java.lang.reflect.Method.invoke(Method.java:507)
11-19 19:56:22.485: E/Database(7403):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:861)
11-19 19:56:22.485: E/Database(7403):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:619)
11-19 19:56:22.485: E/Database(7403):   at dalvik.system.NativeStart.main(Native Method)

我想我找到了导致此错误的代码部分,如果我不再在我的answer.java文件中调用myDbHelper.insertString(...),我就不会再收到错误了..

public void insertString(int id, int level, String field, String player) {

    Cursor c = myDataBase.rawQuery(
            "SELECT * FROM ANSWERS WHERE QUESTION_ID=" + id
                    + " AND PLAYER ='" + player + "'", null);

    myDataBase = this.getWritableDatabase();

    if (c != null) {
        myDataBase.execSQL("UPDATE ANSWERS SET " + field + "=1"
                + " WHERE QUESTION_ID=" + id + " AND LEVEL =" + level
                + " AND PLAYER='" + player + "'");
    } else {
        myDataBase
                .execSQL("INSERT INTO ANSWERS (level, player, question_id, "
                        + field + ") VALUES ("+ level + ",'" + player + "'," + id + ", 1)");
    }

    c.close();
    if (myDataBase != null) {
        myDataBase.close();

    }

}

我试过但没有解决的问题之一是将其添加到我的answer.java文件中:

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (myDbHelper != null) {
                 myDbHelper.close();
            }
return true;
        }

        return super.onKeyDown(keyCode, event);
    } 
我的databasehelper.java中的

//和.close()是:

@Override
public synchronized void close() {
    if (myDataBase != null)
        myDataBase.close();
    if (c != null)
        c.close();
    super.close();
}
希望有人能告诉我光明!

编辑

@sam你的解决方案给了我这个:

11-19 19:52:42.735: E/AndroidRuntime(6982): FATAL EXCEPTION: main
11-19 19:52:42.735: E/AndroidRuntime(6982): android.database.sqlite.SQLiteException: attempt to write a readonly database: UPDATE ANSWERS SET showhint1=1 WHERE QUESTION_ID=18 AND LEVEL =1 AND PLAYER='trst'
11-19 19:52:42.735: E/AndroidRuntime(6982):     at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
11-19 19:52:42.735: E/AndroidRuntime(6982):     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1763)
11-19 19:52:42.735: E/AndroidRuntime(6982):     at com.test.game.db.DataBaseHelper.insertString(DataBaseHelper.java:301)
11-19 19:52:42.735: E/AndroidRuntime(6982):     at com.test.game.Answer.onClick(Answer.java:241)
11-19 19:52:42.735: E/AndroidRuntime(6982):     at android.view.View.performClick(View.java:2491)
11-19 19:52:42.735: E/AndroidRuntime(6982):     at android.view.View$PerformClick.run(View.java:9086)
11-19 19:52:42.735: E/AndroidRuntime(6982):     at android.os.Handler.handleCallback(Handler.java:587)
11-19 19:52:42.735: E/AndroidRuntime(6982):     at android.os.Handler.dispatchMessage(Handler.java:92)
11-19 19:52:42.735: E/AndroidRuntime(6982):     at android.os.Looper.loop(Looper.java:130)
11-19 19:52:42.735: E/AndroidRuntime(6982):     at android.app.ActivityThread.main(ActivityThread.java:3683)
11-19 19:52:42.735: E/AndroidRuntime(6982):     at java.lang.reflect.Method.invokeNative(Native Method)
11-19 19:52:42.735: E/AndroidRuntime(6982):     at java.lang.reflect.Method.invoke(Method.java:507)
11-19 19:52:42.735: E/AndroidRuntime(6982):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:861)
11-19 19:52:42.735: E/AndroidRuntime(6982):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:619)
11-19 19:52:42.735: E/AndroidRuntime(6982):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

显然,您可以在多个位置打开数据库:

// myDataBase is already open or else this could not work
Cursor c = myDataBase.rawQuery(
        "SELECT * FROM ANSWERS WHERE QUESTION_ID=" + id
                + " AND PLAYER ='" + player + "'", null);

// You are opening myDataBase again
myDataBase = this.getWritableDatabase();

只需查看代码,查找getWritableDatabase()getReadableDatabase(),找到您忘记关闭的位置。 如果您经常访问数据库,请在您的活动onResume()中打开数据库一次,然后在onPause()中将其关闭一次。