本文介绍了尝试重新打开已关闭的对象:SQLiteDatabase:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从数据库中删除的东西,然后插入一个新的价值。我不很了解数据库,所以会在什么在这里走错AP preciate建议。

我不断收到以下错误:

 产生的原因:java.lang.IllegalStateException:尝试重新打开一个已关闭的对象:SQLiteDatabase:
 

它从一个异步任务调用,能正常工作的第一次,但打破了第二次任务被触发。

下面是重要的片段:

 公共无效removeAndInsert(字符串configRaw){
    SQLiteDatabase的sqlite = NULL;
    尝试 {
        源码= dbHelper.getWritableDatabase();
        removeAndInsert(configRaw,SQLite的);
    .....
    最后 {
        如果(源码!= NULL){
            sqlite.close();
        }
    }



无效removeAndInsert(字符串configRaw,SQLiteDatabase源码)抛出的SQLException {
    ContentValues​​ initialValues​​ =新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,空,initialValues​​);
}
 

解决方案

试试这个。

 公共无效removeAndInsert(字符串configRaw){
  SQLiteDatabase的sqlite = NULL;
  尝试 {
    源码= dbHelper.getWritableDatabase();
    同步(源码){
      removeAndInsert(configRaw,SQLite的);
    }
  .....
    最后 {
    如果(源码= NULL和放大器;!&安培; sqlite.isOpen()){
        sqlite.close();
    }
}

无效removeAndInsert(字符串configRaw,SQLiteDatabase源码)抛出的SQLException {
  ContentValues​​ initialValues​​ =新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,空,initialValues​​);
}
 

I'm trying to delete something from a database then insert a new value. I don't know much about databases, so would appreciate advice on whats going wrong here.

I keep getting the following error:

Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase:

Its called from an async task, works fine the first time, but breaks the second time that the task is triggered.

Here are the important snippets:

 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);
}
解决方案

Try this.

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);
}

这篇关于尝试重新打开已关闭的对象:SQLiteDatabase:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 19:00