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
Don’t call
getWritableDatabase(). Use the one passed in: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 inonUpgrade(). You callgetWritableDatabase(), which sees an upgrade is needed. Were it not for the check, you’d be right back intoonUpgrade(), ad infinitum.