Getdatabase Called Recursively
My first time asking a question here, so be gentle, Lol. Anyway. Ive been working on an Android and my latest build ran flawlessly.. Until yesterday, when IT gave me a new workst
Solution 1:
Try changing your setDefaultLabel()
method to...
publicvoidsetDefaultLabel(SQLiteDatabase db)
...then in onCreate(...)
simply pass the db
parameter into it and get rid of this line...
SQLiteDatabasedb=this.getWritableDatabase();
Your code should then look like this...
@OverridepublicvoidonCreate(SQLiteDatabase db) {
// Create tables
db.execSQL(CREATE_CATEGORIES_TABLE);
db.execSQL(CREATE_CHRGDATA_TABLE);
db.execSQL(CREATE_SETTINGS_TABLE);
setDefaultLabel(db);
}
/**
*
*/publicvoidsetDefaultLabel(SQLiteDatabase db) {
// create default labelContentValues values = newContentValues();
values.put(KEY_NAME, "Default");
db.insert(TABLE_LABELS, null, values);
}
The problem in your existing code is that onCreate(...)
is being passed a reference to the open / writeable database but it then calls setDefaultLabel(...)
which attempts to get another writeable reference to the database.
Solution 2:
Here is my solution for this: In the Helper, I override 2 methods getWritableDatabase() and getReadableDatabase() like below: Notice that you should not close the database, it may be crashed.
@OverridepublicvoidonCreate(SQLiteDatabase db) {
// All your code here .....// Add default value for all tables
isCreating = true;
currentDB = db;
generateAllDefaultData();
// release var
isCreating = false;
currentDB = null;
}
boolean isCreating = false;
SQLiteDatabase currentDB = null;
@OverridepublicSQLiteDatabasegetWritableDatabase() {
// TODO Auto-generated method stubif(isCreating && currentDB != null){
return currentDB;
}
returnsuper.getWritableDatabase();
}
@OverridepublicSQLiteDatabasegetReadableDatabase() {
// TODO Auto-generated method stubif(isCreating && currentDB != null){
return currentDB;
}
returnsuper.getReadableDatabase();
}
Post a Comment for "Getdatabase Called Recursively"