【发布时间】:2012-02-28 09:22:23
【问题描述】:
在 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() 上。
这真的让我很困惑,所以有人可以帮我解决问题吗?
【问题讨论】:
-
loadLevelData(db) 的放置代码;
-
你能发布你制作的完整的数据库助手类吗?
-
当你关闭数据库时,你需要在对数据库进行任何其他调用之前打开()。看来您没有调用 open()
-
我相信 @Waqas 无关紧要,只有一些插入语句。并且 thinksteep,getWritableDatabase() 抛出第二个异常,所以我唯一可以放置 open() 的地方是紧跟在 close() 之后。
标签: android database sqlite exception illegalstateexception