我发布了我的应用程序的更新,并从用户那里收到大量错误,我无法重新创建它或查明问题。
我得到两个错误:
java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase
java.lang.IllegalStateException: database not open
错误发生在以下代码中的某处:
public DBUserC getUser(int _id){
synchronized (DBAdapter.LOCK){
M.openDB(context);
Cursor cursor = M.database.query(DBUserHelper.TABLE_USERS,
VUser.allColumns, DBUserHelper.COLUMN_ID + " = '"+_id+"'", null, null, null, null);
DBUserC user;
if (cursor.moveToFirst()){
user = cursorToInfo(cursor);
} else{
user = newUser();
}
cursor.close();
M.closeDB();
return user;
}
}
在这个版本中,有一个数据库升级,其中我通过db.execSql
. 它不在单独的线程中。
在每次调用数据库时(除了在 中onUpgrade
),我同步,然后打开它,运行我的代码,然后关闭它。在此升级之前我没有任何问题,并且找不到问题。
任何帮助将不胜感激。
编辑:要打开我的数据库,我会:
if (helper==null)
helper = new DBAdapter(context);
if (database==null){
database = helper.getWritableDatabase();
} else if (!database.isOpen())
database = helper.getWritableDatabase();
并关闭:
helper.close();
helper = null;
database = null;
获取信息的示例方法:
public DBUserC getUser(int _id){
synchronized (DBAdapter.LOCK){
openDB(context);//this is the open code above
Cursor cursor = M.database.query(DBUserHelper.TABLE_USERS,
VUser.allColumns, DBUserHelper.COLUMN_ID + " = '"+_id+"'", null, null, null, null);
DBUserC user;
if (cursor.moveToFirst()){
user = cursorToInfo(cursor);//does not contain DB operations
} else{
user = newUser(); ////does not contain Database operations
}
cursor.close();
closeDB();//This is the close code above
return user;
}
}
AHost.onCreate
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
M.initializeDB(context); //synchronized call to the openDB code I posted above
M.openDataDB(context); //opens a different database by a different file name. This DB is not an issue
//irrelevant ui setup
int _id = pref.getInt(P.eLastUser, VUser.ID_NEW);//row id of user
M.requeryUser();//synchronized, access database
M.switchUser(_id);//synchronized, access database
}