Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getWritableDatabase called recursively

Tags:

android

sqlite

I'm attempting to update my database table with the following code:

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        String query = "ALTER TABLE names ADD COLUMN hidden integer default 0";
        dbHelper.getWritableDatabase().rawQuery(query, null);

    }

However, when I start the application and it tries to upgrade the database I get the following exception:

    ...Caused by: java.lang.IllegalStateException: getWritableDatabase called recursively

Does anyone know how I can get around this issue and what exactly is causing it?

Thanks

like image 947
Nick Avatar asked Feb 05 '12 20:02

Nick


1 Answers

Don't call getWritableDatabase(). Use the one passed in:

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    String query = "ALTER TABLE names ADD COLUMN hidden integer default 0";
    db.rawQuery(query, null);

}

Why? When you call getWritableDatabase() the OpenHelper is detecting that the database needs to be updated, hence it starts up the recursion warning you're seeing. In other words, you're in onUpgrade(). You call getWritableDatabase(), which sees an upgrade is needed. Were it not for the check, you'd be right back into onUpgrade(), ad infinitum.

like image 131
Brian Dupuis Avatar answered Sep 28 '22 09:09

Brian Dupuis