I am trying to upgrade sqlite db by add more data to the existing db. The db is installed on user's phone. My db version is currently 1 and am trying to upgrade to 2. I didn't create onCreate method because I already have sqlite db inside assets folder which is already created by other app. I am new to sqlite. I googled this error but I don't know how to resolve this error. Any help would be appreciated.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.donghyouny.biblecard, PID: 10475
java.lang.IllegalStateException: getDatabase called recursively
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:431)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:391)
at com.donghyouny.biblecard.MyDatabaseHelper.insertToBible(MyDatabaseHelper.java:68)
at com.donghyouny.biblecard.MyDatabaseHelper.onUpgrade(MyDatabaseHelper.java:62)
class MyDatabaseHelper extends SQLiteOpenHelper {
private Context context;
private static final String DATABASE_NAME = "bible.db";
private static final int DATABASE_VERSION = 2;
private static final String TABLE_SAVE = "save";
private static final String TABLE_BIBLE = "bible";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_TIMESTAMP = "TimeStamp";
private static final String COLUMN_BIBLETYPE = "BibleType";
private static final String COLUMN_VERSE = "Verse";
private static final String COLUMN_CONTENT = "Content";
private static final String COLUMN_NUM = "Num";
private static final String COLUMN_CNUM = "CNum";
private static final String COLUMN_CHECKNUM = "CheckNum";
private static final String COLUMN_IMAGE = "Image";
private static final String COLUMN_BIBLEID = "BibleId";
MyDatabaseHelper(@Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
if(oldversion==1){
insertToBible("Bible", "Psalm 1,1-2", "I am the way and truth",9, 3, 0, imageViewToByte(28));
}
}
void insertToBible(String bibleType, String verse, String content, int num, int cnum, int checknum, byte[] image){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COLUMN_BIBLETYPE, bibleType);
cv.put(COLUMN_VERSE, verse);
cv.put(COLUMN_CONTENT, content);
cv.put(COLUMN_NUM, num);
cv.put(COLUMN_CNUM, cnum);
cv.put(COLUMN_CHECKNUM, checknum);
cv.put(COLUMN_IMAGE, image);
cv.put(COLUMN_TIMESTAMP, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
long result = db.insert(TABLE_SAVE,null, cv);
if(result == -1){
}else {
}
}
Change the insertToBible()
's signature to include, as 1st argument, an instance of SQLiteDatabase
and remove:
SQLiteDatabase db = this.getWritableDatabase();
so it is like this:
void insertToBible(SQLiteDatabase db, String bibleType, String verse, String content, int num, int cnum, int checknum, byte[] image)
When you call it use onUpgrade()
's instance of SQLiteDatabase
:
insertToBible(db, "Bible", "Psalm 1,1-2", "I am the way and truth",9, 3, 0, imageViewToByte(28));