【问题标题】:Android: runtime exception database already closedAndroid:运行时异常数据库已经关闭
【发布时间】:2015-09-28 10:51:10
【问题描述】:

我正在使用 SQLite 数据库获取表的记录,如果存在特定记录。运行应用程序时总是出错 = 原因:java.lang.IllegalStateException:数据库/data/data/com.example .tazeen.xxx/databases/xx(conn# 0) 已经关闭

 dbhelper = new MyDbHelper(this);
        SQLiteDatabase db1 = dbhelper.getReadableDatabase();

        Cursor cursor = db1.rawQuery("select * from ActivityObjectList", null);

        if (cursor.moveToFirst()) {
            do {
                imageName = cursor.getString(cursor.getColumnIndex("imageaudioPath"));
                String strDownLoadStatus = cursor.getString(cursor.getColumnIndex("DownLoad_Status"));
 } while (cursor.moveToNext());
        }
        cursor.close();

       db1.close();
    }

【问题讨论】:

  • 在应用程序级别创建数据库实例
  • 你能知道异常在哪一行抛出。

标签: android


【解决方案1】:

我相信你应该关闭类似 dbhelper.close()... 而不是 db1.close();

通过理解你的代码sn-p来猜测。

【讨论】:

    【解决方案2】:

    你应该在 db helper 类中关闭你的 sqlite db 对象

    @Override
    public synchronized void close() {
    
        if (mDb != null)
            mDb.close();
    
        super.close();
    }
    

    【讨论】:

      【解决方案3】:

      你应该在读完表后关闭 sqlite 数据库。 你会用 像这样

      试试{ 在此输入代码 what do to } finally{ db.close(); }

      【讨论】:

        猜你喜欢
        • 2011-10-29
        • 2012-09-07
        • 1970-01-01
        • 2012-04-08
        • 1970-01-01
        • 1970-01-01
        • 2011-09-19
        • 2012-10-19
        • 2014-11-21
        相关资源
        最近更新 更多