问题描述
我试图用下面的code更新我的数据库表:
I'm attempting to update my database table with the following code:
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "ALTER TABLE names ADD COLUMN hidden integer default 0";
dbHelper.getWritableDatabase().rawQuery(query, null);
}
然而,当我启动应用程序,并尝试升级我得到以下异常数据库:
However, when I start the application and it tries to upgrade the database I get the following exception:
...Caused by: java.lang.IllegalStateException: getWritableDatabase called recursively
有谁知道我怎么能解决这个问题,到底是什么造成的呢?
Does anyone know how I can get around this issue and what exactly is causing it?
感谢
推荐答案
不要叫 getWritableDatabase()
。使用在突破一:
Don't call getWritableDatabase()
. Use the one passed in:
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "ALTER TABLE names ADD COLUMN hidden integer default 0";
db.rawQuery(query, null);
}
为什么呢?当你调用 getWritableDatabase()
的OpenHelper正在检测数据库需要更新,因此它启动了递归警告你看到。换句话说,你在 onUpgrade()
。你叫 getWritableDatabase()
,它认为升级是必要的。如果不是支票,你是对的回 onUpgrade()
,循环往复。
Why? When you call getWritableDatabase()
the OpenHelper is detecting that the database needs to be updated, hence it starts up the recursion warning you're seeing. In other words, you're in onUpgrade()
. You call getWritableDatabase()
, which sees an upgrade is needed. Were it not for the check, you'd be right back into onUpgrade()
, ad infinitum.
这篇关于getWritableDatabase递归调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!