DenisD
DenisD

Reputation: 670

Finalizing a Cursor that has not been deactivated or closed after update/insert

I got the most common error when I worked with SQLite. Unfortunately, I couldn’t find answer for my question, because when this error is happen I’m not using a cursor. This error appears after insert+update. There is the error text:

09-16 08:03:33.253: E/Cursor(1436): Finalizing a Cursor that has not been deactivated or closed. database = /data/data/*******/databases/settings.db, table = null, query = select * from preferences where pref_name = 'preference'
09-16 08:03:33.253: E/Cursor(1436): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
09-16 08:03:33.253: E/Cursor(1436):     at android.database.sqlite.SQLiteCursor.<init>(SQLiteCursor.java:210)
09-16 08:03:33.253: E/Cursor(1436):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:53)
09-16 08:03:33.253: E/Cursor(1436):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
09-16 08:03:33.253: E/Cursor(1436):     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1315)
09-16 08:03:33.253: E/Cursor(1436):     at ****.db.SettingsSQLiteHelper.updateValue(SettingsSQLiteHelper.java:132)
09-16 08:03:33.253: E/Cursor(1436):     at ****.PreferencesActivity$1.onClick(PreferencesActivity.java:67)
09-16 08:03:33.253: E/Cursor(1436):     at android.view.View.performClick(View.java:2408)
09-16 08:03:33.253: E/Cursor(1436):     at android.view.View$PerformClick.run(View.java:8816)
09-16 08:03:33.253: E/Cursor(1436):     at android.os.Handler.handleCallback(Handler.java:587)
09-16 08:03:33.253: E/Cursor(1436):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-16 08:03:33.253: E/Cursor(1436):     at android.os.Looper.loop(Looper.java:123)
09-16 08:03:33.253: E/Cursor(1436):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-16 08:03:33.253: E/Cursor(1436):     at java.lang.reflect.Method.invokeNative(Native Method)
09-16 08:03:33.253: E/Cursor(1436):     at java.lang.reflect.Method.invoke(Method.java:521)
09-16 08:03:33.253: E/Cursor(1436):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-16 08:03:33.253: E/Cursor(1436):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-16 08:03:33.253: E/Cursor(1436):     at dalvik.system.NativeStart.main(Native Method)

There is the code, after its execution, when the app is trying to do any sql I get the error, which was described above:

public boolean updateValue(String property, String value) {
        SQLiteDatabase database = getWritableDatabase();
        try {
            ContentValues cv = new ContentValues();
            cv.put(COLUMN_PREFNAME, property);
            cv.put(COLUMN_PREFVALUE, value);            
            if (database.rawQuery(
                    "select * from " + TABLE_PREFERENCES + " where "
                            + COLUMN_PREFNAME + " = '" + property + "'", null)
                    .getCount() < 1) {
                Log.i("MyLog", "There wasn't property " + property
                        + " and system inserted new row");
                database.insert(TABLE_PREFERENCES, null, cv);
            } else {
                cv.clear();
                cv.put(COLUMN_PREFVALUE, value); 
                database.update(TABLE_PREFERENCES, cv, COLUMN_PREFNAME
                        + "='" + property + "'", null);
            }
            database.close();
        } catch (Exception e) {
            Log.e("MyLog", "Error in SettingsDB.updateValue :: " + e);
            database.close();
            return false;
        }
        return true;
    }

To sum up, first start of the app executes the method, which was described above, to set up preferences of application. After this method, the app cannot run any SQL due to the error. When I restart application, it works well, because the app doesn’t start the method again. I believe there is some kind of invisible cursor, which I didn’t close. Could you please advice me, what should I add or close to remove this error? Thank you!

Upvotes: 0

Views: 884

Answers (2)

Shakeeb Ayaz
Shakeeb Ayaz

Reputation: 6096

As ianhanniballake said rawQuery returns a Cursor that you are not closing.But you are not using any cursor variable so better you should close your database in finally block not in try block

Upvotes: 1

ianhanniballake
ianhanniballake

Reputation: 199825

rawQuery returns a Cursor that you are not closing.

Upvotes: 0

Related Questions