【问题标题】:android-SQLCipher -Application did not close the cursor or database object that was opened hereandroid-SQLCipher -Application 没有关闭此处打开的游标或数据库对象
【发布时间】:2014-07-06 15:47:23
【问题描述】:

我以前有一个数据库,没有错误,我的数据库来自asset文件夹,已经用SQLCipher编码了

之后,在我的所有活动中,我都会收到游标或数据库已打开但未关闭的错误。

@Override
protected void onCreate() {
    super.onCreate();
    myDb = new DBAdapter(this);
    myDb.open();
    cursor = myDb.getFavorits();
}
@Override
protected void onDestroy() {
    super.onDestroy();
    myDb.close();
    cursor.close();
}
@Override
protected void onStop() {
    super.onStop();
        myDb.close();
        cursor.close();
}

这是它打开和关闭的 DBAdapter 类:

public void open() {
    SQLiteDatabase.loadLibs(context);
    File databaseFile = context.getDatabasePath("mydb");
    db = SQLiteDatabase.openOrCreateDatabase(databaseFile,"mypass", null);

    }
public void close() {
    Log.v("this","dbclose");
    db.close(); 
}

public Cursor getFavorits() {
    Cursor c = db.query(DATABASE_TABLE, ALL_KEYS, null, null, null, null,null, null);
    if (c != null) {
        c.moveToFirst();
    }

    return c;
}

但是当我离开这个活动时,它会告诉我这个错误:

 Finalizing a Cursor that has not been deactivated or closed. database = /data/data/pachagename/databases/mydb, table = null, query = SELECT _id from favorits
07-06 15:42:42.769: E/Cursor(1214): net.sqlcipher.database.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here

您能帮我找出并解决这个问题吗?

更新: 我现在注意到一些新的东西,当我进入活动而不离开活动时,它显示了这个错误!我该怎么办 ?

【问题讨论】:

    标签: android android-sqlite sqlcipher


    【解决方案1】:

    onDestroyonStop 之后调用。我相信您正在尝试关闭已经关闭的东西。尝试在 onDestroy 方法中只保留 super.onDestroy();

    编辑:只是在黑暗中拍摄:

    当您将 getFavorits() 更改为 private static method 并像这样从 onCreate 调用它时会发生什么:cursor = DBAdapter.getFavorits()

    【讨论】:

    • 它没有关闭,否则它不应该显示这个错误。顺便说一句,我试试你说的,没用
    • 鉴于您的新信息,我猜 onStop 和 onDestroy 不是问题(至少现在还没有)。请张贴getFavorits() 方法的代码。问题可能就在那里。
    【解决方案2】:

    我相信您的问题是您在关闭光标之前关闭了数据库。当游标访问数据库时,应在数据库之前关闭游标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-04
      • 1970-01-01
      • 2015-11-26
      相关资源
      最近更新 更多