Android: SqLite database illegal state exception (database already closed)
android
Solution
You need to instantiate your dbhelper class in `onResume`... that too (could possibly) get destroyed when you go away from the activity.
dbHelper = new YourDatabaseHelper();
Actually, since `onResume` is called even on a new start, you could move all your code there (and thus avoid instantiating you helper (and doing other things) twice.
Problem
I have a database helper object. It is important I close it when leaving the activity, so I do. However, on returning to the activity I get a warning about querying a database that has already been closed. The query actually works, but there's still something obviously wrong. My understanding of the Android activity life cycle is not strong, but I would assume I need to reopen the database in onResume(). Is this not correct? Here is onResume(): ``` @Override protected void onResume() { super.onResume(); dbHelper.setInteger(playerID); dbHelper.openDataBase(); ourCursor = dbHelper.getPlayerSavedQuestions(); startManagingCursor(ourCursor); adapter = new MyCustomAdapter(ourCursor); myListView.setAdapter(adapter); } ``` Here is dbHelper.openDataBase(); ``` public void openDataBase() throws SQLException { String myPath = DATABASE_PATH + DATABASE_NAME; dbSqlite = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); } ``` Here is my logcat: ``` 06-15 13:51:32.388: W/SQLiteCursor(26750): requery() failed database /data/data/com.boboshi.exquizit.eng/databases/exquizit_custom.mp3 (conn# 0) already closed 06-15 13:51:32.388: W/SQLiteCursor(26750): java.lang.IllegalStateException: database /data/data/com.boboshi.exquizit.eng/databases/exquizit_custom.mp3 (conn# 0) already closed 06-15 13:51:32.388: W/SQLiteCursor(26750): at android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2082) 06-15 13:51:32.388: W/SQLiteCursor(26750): at android.database.sqlite.SQLiteDatabase.getDbConnection(SQLiteDatabase.java:2407) 06-15 13:51:32.388: W/SQLiteCursor(26750): at android.database.sqlite.SQLiteDatabase.getDatabaseHandle(SQLiteDatabase.java:2388) 06-15 13:51:32.388: W/SQLiteCursor(26750): at android.database.sqlite.SQLiteCursor.requery(SQLiteCursor.java:246) 06-15 13:51:32.388: W/SQLiteCursor(26750): at android.app.Activity.performRestart(Activity.java:4505) 06-15 13:51:32.388: W/SQLiteCursor(26750): at android.app.Activity.performResume(Activity.java:4531) 06-15 13:51:32.388: W/SQLiteCursor(26750): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2434) 06-15 13:51:32.388: W/SQLiteCursor(26750): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2472) 06-15 13:51:32.388: W/SQLiteCursor(26750): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1173) 06-15 13:51:32.388: W/SQLiteCursor(26750): at android.os.Handler.dispatchMessage(Handler.java:99) 06-15 13:51:32.388: W/SQLiteCursor(26750): at android.os.Looper.loop(Looper.java:137) 06-15 13:51:32.388: W/SQLiteCursor(26750): at android.app.ActivityThread.main(ActivityThread.java:4424) 06-15 13:51:32.388: W/SQLiteCursor(26750): at java.lang.reflect.Method.invokeNative(Native Method) 06-15 13:51:32.388: W/SQLiteCursor(26750): at java.lang.reflect.Method.invoke(Method.java:511) 06-15 13:51:32.388: W/SQLiteCursor(26750): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 06-15 13:51:32.388: W/SQLiteCursor(26750): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 06-15 13:51:32.388: W/SQLiteCursor(26750): at dalvik.system.NativeStart.main(Native Method) ```