GetWritableDatabase called recursively

I am trying to update a 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 run the application and try to update the database, I get the following exception:

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

Does anyone know how I can get around this problem and what exactly causes it?

thanks

+6
source share
1 answer

Do not call getWritableDatabase() . Use the one that was 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() , OpenHelper detects that the database needs to be updated, so it triggers the recursion warning that you see. In other words, you are in onUpgrade() . You call getWritableDatabase() , which sees that an update is needed. If not for verification, you will return to onUpgrade() , ad infinitum.

+23
source

Source: https://habr.com/ru/post/907719/


All Articles