IllegalStateException数据库已经关闭

IllegalStateException数据库已经关闭

本文介绍了Android的java.lang.IllegalStateException数据库已经关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很奇怪的问题,我不能够重现,也不在仿真器或任何被测试的Andr​​oid设备。

I have a really weird problem that i am not able to reproduce neither on emulator or on any of the tested android devices.

我有一个包含所有矿山的数据访问功能的SQLite数据库控制器,在这个类中,我有一个简单的函数来检查表是否存在:

I have a SQlite Database controller that holds all of mine data access functions, in this class i have a simple function to check if a table exists:

public boolean tableExists(String tableName) {
  if(db==null || !db.isOpen()) db =  dbhelper.getWritableDatabase();
  Cursor cursor = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='"+tableName+"'", null);
       if(cursor!=null) {
           if(cursor.getCount()>0) {
              cursor.close();
              return true;
             }
            cursor.close();
          }
      return false;
    }

在我的应用程序开始我创建一个异步任务来检查更新,并在此过程中,我将检查本地表在数据库中存在。

On my application start i create an async task to check for updates and on this process i will check for the existence of a local table in the database.

引发的错误是在光标光标= db.rawQuery(...行,错误是:

The error thrown is on the Cursor cursor = db.rawQuery(... line, and the error is:

java.lang.IllegalStateException:数据库... db.sqlite已经关闭

java.lang.RuntimeException: An error occured while executing doInBackground()
 at android.os.AsyncTask$3.done(AsyncTask.java:200)
 at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
 at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
 at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
 at java.util.concurrent.FutureTask.run(FutureTask.java:138)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
 at java.lang.Thread.run(Thread.java:1019)
Caused by: java.lang.IllegalStateException: database /data/data/com.xxx.xxx.xxx.android/databases/db.sqlite already closed
 at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:59)
 at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
 at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
 at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
 at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1364)
 at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1332)
 at com.xxx.xxx.xxx.utils.SQLiteController.tableExists(SQLiteController.java:1463)

我不能够重现此错误,但根据ACRA这是发生多次在不同的设备。

I am not able to reproduce this error but according to ACRA this is happening several times in different devices.

有人可以给我一盏灯呢?

Can someone give me a light on this?

感谢。

推荐答案

请把那第二个 cursor.close(); 里面的其他块如下

Please put that second cursor.close(); inside the else blocks follows

public boolean tableExists(String tableName) {
  if(db==null || !db.isOpen()) db =  dbhelper.getWritableDatabase();
  Cursor cursor = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table' AND name='"+tableName+"'", null);
       if(cursor!=null) {
           if(cursor.getCount()>0) {
              cursor.close();
              return true;
             }
          else{
                  cursor.close();
              }
             }
      return false;
    }

这篇关于Android的java.lang.IllegalStateException数据库已经关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 17:56