我正在尝试从数据库中删除一些内容然后插入一个新值。我对数据库知之甚少,所以希望得到有关这里出了什么问题的建议。
我不断收到以下错误:
Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase:
它从异步任务中调用,第一次工作正常,但第二次触发任务时中断。
以下是重要的 fragment :
public void removeAndInsert(String configRaw) {
SQLiteDatabase sqlite = null;
try {
sqlite = dbHelper.getWritableDatabase();
removeAndInsert(configRaw, sqlite);
.....
finally {
if (sqlite != null) {
sqlite.close();
}
}
void removeAndInsert(String configRaw, SQLiteDatabase sqlite) throws SQLException {
ContentValues initialValues = new ContentValues();
initialValues.put(DBOpenHelper.CONFIG_CACHE_RAW_DATA, configRaw);
sqlite.delete(DBOpenHelper.CONFIG_CACHE_TABLE_NAME, null, null);
sqlite.insert(DBOpenHelper.CONFIG_CACHE_TABLE_NAME, null, initialValues);
}
最佳答案
试试这个。
public void removeAndInsert(String configRaw) {
SQLiteDatabase sqlite = null;
try {
sqlite = dbHelper.getWritableDatabase();
synchronized(sqlite) {
removeAndInsert(configRaw, sqlite);
}
.....
finally {
if (sqlite != null && sqlite.isOpen()) {
sqlite.close();
}
}
void removeAndInsert(String configRaw, SQLiteDatabase sqlite) throws SQLException {
ContentValues initialValues = new ContentValues();
initialValues.put(DBOpenHelper.CONFIG_CACHE_RAW_DATA, configRaw);
sqlite.delete(DBOpenHelper.CONFIG_CACHE_TABLE_NAME, null, null);
sqlite.insert(DBOpenHelper.CONFIG_CACHE_TABLE_NAME, null, initialValues);
}
关于android - 试图重新打开一个已经关闭的对象 : SQLiteDatabase:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19034391/