Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intermittent SQLiteException: not an error at dbopen

In my app I'm using DB in many situations, but there is one situation in which I'm getting an exception, not every time and could reproduce it (yet).

This is happening only on OS versions 2.3.7 and 2.1-update-1.

The code:

public void removeOldOccurrences() {
        SQLiteDatabase db = dbHelper.getWritableDatabase();
        Long oldTime = System.currentTimeMillis() - VALID_OCCURRENCE_TIME;
        String query = "";
        try {
            query = "DELETE FROM " + LOCATIONS_TABLE + " WHERE not ("
                    + REMEMBERED_FIELD + "=1) " + "and (" + LAST_FIELD + "<"
                    + oldTime + ");";
            db.execSQL(query);
        } catch (Exception e) {
            Log.e(TAG, query);
            e.printStackTrace();
        } finally {
            if (db != null) {
                db.close();
            }
        }
    }

The exception trace is:

android.database.sqlite.SQLiteException: not an error
at android.database.sqlite.SQLiteDatabase.dbopen(Native Method)
at android.database.sqlite.SQLiteDatabase.(SQLiteDatabase.java:1849)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854)
at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847)
at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:573)
at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)

please help.

like image 736
Bush Avatar asked May 06 '12 11:05

Bush


2 Answers

I have also received this same error when passing an empty string for query.

like image 178
Xeridea Avatar answered Nov 10 '22 02:11

Xeridea


This error is coming from the statement getWritableDatabase where you are trying to create / open database.

From the code segment what you have given , i see that you are trying to open the database for each operation.

This is not the right way to do it. You have to open your DB only once before you close it . Normal practise is open it during your other initialisation and take care to close it before exit the app . Once open store the db context and use that in all other operations

for eg : you can have a database manegr class like this :

public DataBaseManager open() throws SQLException {
    try {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();

    } catch (Exception ex) {
        ex.printStackTrace();
        Utils.getHomeActivityinstance().finish();

    }
    return this;
}

now when delte need to be called, do not call open again

public boolean deleteRow(String tableName, long rowId) {

    return mDb.delete(tableName, ID + "=" + rowId, null) > 0;
}
like image 4
png Avatar answered Nov 10 '22 01:11

png