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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With