应用程序没有关闭光标,尽管关闭

时间:2015-02-24 17:31:27

标签: android sqlite cursor

我的Android应用程序中有一个SQLite数据库。当我执行一个操作时,我总是得到一个尚未停用或关闭的Finalizing a Cursor。这是方法:

public String getKey(){
    net.sqlcipher.database.SQLiteDatabase db = this.getWritableDatabase(Login.authPass);
    String selectQuery = "SELECT  * FROM " + STABLE;
    Cursor cursor = db.rawQuery(selectQuery, null); //This is the line that throws the exception
    String s = cursor.getString(1);
    cursor.close();
    db.close();
    return s;
}

这真的令人沮丧,因为我要关闭Cursor,它只是在它到达之前抛出异常。另外,我不知道我在这一行上所做的任何事情都被认为是最终确定了光标。谢谢。

编辑添加Logcat:

02-24 12:28:26.522 12756-12765 / com.itsmr.dre2k14 E / Cursor:完成尚未停用或关闭的光标。 database = /data/data/com.itsmr.dre2k14/databases/keys.db,table = null,query = SELECT * FROM keys     net.sqlcipher.database.DatabaseObjectNotClosedException:应用程序未关闭此处打开的游标或数据库对象             在net.sqlcipher.database.SQLiteCursor。(SQLiteCursor.java:217)             at net.sqlcipher.database.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:53)             at net.sqlcipher.database.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1447)             at net.sqlcipher.database.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1416)             在com.itsmr.dre_android_clean.KeyTable.getKey(KeyTable.java:41)             在com.itsmr.dre_android_clean.Login.noConnectValidate(Login.java:218)             在com.itsmr.dre_android_clean.Login.connect(Login.java:97)             at java.lang.reflect.Method.invokeNative(Native Method)             在java.lang.reflect.Method.invoke(Method.java:515)             在android.view.View $ 1.onClick(View.java:3964)             在android.view.View.performClick(View.java:4640)             在android.view.View $ PerformClick.run(View.java:19421)             在android.os.Handler.handleCallback(Handler.java:733)             在android.os.Handler.dispatchMessage(Handler.java:95)             在android.os.Looper.loop(Looper.java:136)             在android.app.ActivityThread.main(ActivityThread.java:5476)             at java.lang.reflect.Method.invokeNative(Native Method)             在java.lang.reflect.Method.invoke(Method.java:515)             在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1268)             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)             在dalvik.system.NativeStart.main(本地方法)

1 个答案:

答案 0 :(得分:1)

看起来你应该调用moveToFirst()将光标移动到数据库的第一行。

此外,只需确保这是您的意图,调用cursor.getString(1);将获得当前行中的第二列,因为它基于零。 http://developer.android.com/reference/android/database/Cursor.html#getString(int)

此外,最佳做法是在使用游标之前确保游标不为null,同时使用try / catch进行数据库操作。

public String getKey(){
 String s = null;

 try{
  net.sqlcipher.database.SQLiteDatabase db = this.getWritableDatabase(Login.authPass);
  String selectQuery = "SELECT  * FROM " + STABLE;
  Cursor cursor = db.rawQuery(selectQuery, null);
  if (cursor != null && cursor.moveToFirst()){
    s = cursor.getString(1);
  }

 } catch (Exception ex) {
    Log.e("MyApp", ex.getMessage());
 } finally
 {
    if (cursor != null) {
       cursor.close();

    }
    if (db != null) {
        db.close();
    }

 }

return s;
}

这篇文章也许有帮助: cursor.getstring() is getting the wrong field in the database