android - 试图重新打开一个已经关闭的对象 : SQLiteDatabase:

标签 android sqlite

我正在尝试从数据库中删除一些内容然后插入一个新值。我对数据库知之甚少,所以希望得到有关这里出了什么问题的建议。

我不断收到以下错误:

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/

相关文章:

Android 单元测试 - 如何在与应用程序相同的项目中运行测试?

android - 将触摸传递给下的 View

android - sqlite 日期查询

java - 在 Android 中返回 SQLite 表的所有列

android - ADB 在连接时将 Motorola Xoom 显示为离线

android - 在 android ImageView 上创建一个 "drag and clear mask"

java - 如何保护 Android 中的 Activity

android - 从电池性能影响的角度来看,SQLite 数据库与基于文件的数据存储

sqlite - 从 SQLite 中的表中获取之前的日期和时间

python - ( "SELECT password FROM peerlist WHERE username=?",用户名1)