在 Android 中创建和填充我的新 SQLiteDatabase 时,我遇到了两个相互矛盾的异常。简而言之,我的代码:
SQLiteOpenHelper 扩展类:
public void onCreate(SQLiteDatabase db) {
db.execSQL(DB_TABLE_CREATE);
loadLevelData(db); //puts data in the database
//db.close(); <<< ?
}
在我的活动类中,我实例化了这个类(在 onCreate() 中),并调用 getWritableDatabase():
dbHelper = new DbOpenHelper(getApplicationContext());
database = dbHelper.getWritableDatabase();
db.close()
现在,如果我在填充数据库后 不调用,就像上面一样,我得到
android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
但是,如果我关闭它,我会收到以下异常:
java.lang.IllegalStateException: database not open
在 getWritableDatabase() 上。
这真的让我感到困惑,所以任何人都可以帮助我解决问题吗?