0

Here is my code:

public class DBHelper extends SQLiteOpenHelper {

@Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("sql-query");

        try {
            //db = getWritableDatabase();
            fillAllDB(db);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
// some code
}

fillAllDB(db); method adds a record to the table, but I can't use db = getWritableDatabase(); before method call - this is causing the looping. What to do in this case?

Mark Korzhov
  • 2,109
  • 11
  • 31
  • 63

2 Answers2

2

OnCreate method gives you writable database only, so no need to get the writable database, when you call the writable/readable database, sqlite framework checks whether database exist or not, if not then it will call the onCreate method, that why this is recursive, you should not call getWritable/getReadable database in onCreate/onUpgrade method of the SqliteOpenHelper class

Kapil Vats
  • 5,485
  • 1
  • 27
  • 30
1

Use the SQLiteDatabase db argument passed to your onCreate() method as the database to call your operations on.

getWritableDatabase() and getReadableDatabase() will invoke your onCreate() in case the database file did not exist, and calling get...Database() recursively while the previous call is still being processed causes this exception.

Also, catching exceptions in onCreate() is not a good idea. If there's a problem, the onCreate() method should not return normally - that tells the framework that the database setup was successful.

laalto
  • 150,114
  • 66
  • 286
  • 303