Note that there are some explanatory texts on larger screens.

plurals
  1. PODatabase is not closed error altough closed in code
    primarykey
    data
    text
    <p>I am having a issue where I am strugling with for a few days now. It keeps giving me an <strong>error that my databased wasn't closed</strong>. I get items out of my sql db and put them in a gridview with a custom adapter. If you click on an item, it will take you to another class (answer.java). Doing nothing and hitting the back button won't cause an error, but if I call a certain function and hit the back button after this function, I get the close() error <strong>when pressing the back button</strong>. So this is the exact error:</p> <pre><code> 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.&lt;init&gt;(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) </code></pre> <p>I think I found the part of the code which is causing this error, if I don't call myDbHelper.insertString(...) in my answer.java file anymore, I don't get the error anymore...</p> <pre><code>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(); } } </code></pre> <p>One of the things I tried but didn't solve it was adding this to my answer.java file:</p> <pre><code>@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); } </code></pre> <p>// and .close() in my databasehelper.java is:</p> <pre><code>@Override public synchronized void close() { if (myDataBase != null) myDataBase.close(); if (c != null) c.close(); super.close(); } </code></pre> <p>Hope somebody can show me the light! </p> <p>EDIT </p> <p>@sam your solution gives me this:</p> <pre><code>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) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload