如何避免db not close和游标异常

时间:2011-02-02 09:18:54

标签: android database cursor

02-02 14:31:34.048: WARN/SQLiteCompiledSql(359): Releasing statement in a finalizer. Please ensure that you explicitly call close() on your cursor: SELECT * FROM 
02-02 14:31:34.048: WARN/SQLiteCompiledSql(359): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
02-02 14:31:34.129: ERROR/Database(359): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
02-02 14:31:34.129: ERROR/Database(359):     at 

如何避免这种异常?请帮忙

我的代码如下:

 **DataSQLHelper .class**

public class DataSQLHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "test.db";
private static final int DATABASE_VERSION = 1;

   public DataSQLHelper (Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}



@Override
public void onCreate(SQLiteDatabase db) {
    String sql = "create table " + TABLE + "( " + BaseColumns._ID
    + " integer primary key autoincrement, " + ID + " text, "
    + PASSWORD + " text, " + ACTIVE + " text, " + STATUS
    + " text);";
    db.execSQL(sql);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion >= newVersion)
    return;

String sql = null;
if (oldVersion == 1)
    sql = "alter table " + TABLE + " add note text;";
if (oldVersion == 2)
    sql = "";


if (sql != null)
    db.execSQL(sql);
}

        @Override
    public synchronized void close() {
                   super.close();


    }

}


    ***Test_Java .java***

     public class Test_Java extends Activity {
  DataSQLHelper helData;
SQLiteDatabase db;
Cursor cursor;


@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try {      

        helData= new DataSQLHelper(this);
     cursor = getData();
    startManagingCursor(cursor);

    setContentView(R.layout.view);


} catch (Exception ex) {

    }

    } // onCreate Ends



private Cursor getData() {
try {
     db = helData.getReadableDatabase();
     cursor = db.query(DataSQLHelper.TABLE, null, null,
        null, null, null, null);
    startManagingCursor(cursor);
    return cursor;
} catch (Exception ex) {
    System.out.println("Exception Occured : " + ex.toString());
    return null;
}

}

@Override
protected void onDestroy() {
   System.out.println("onDestroy");
    super.onDestroy();
    if (db!=null){
                   db.close();
                }
    if (cursor!=null){
                    cursor.close();
    }

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

}
}

2 个答案:

答案 0 :(得分:4)

您应该在onDestroy()方法中反转close语句。 首先关闭光标,然后关闭db:

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

您基本上需要颠倒创建/打开db和cursor的顺序。

另请注意,示例中的db / cursor是在onCreate()中打开的,这是来自系统的回调。您可能希望在离开该方法之前关闭光标/ db。您可以自由地将DataSQLHelper缓存在您的 应用

在我的应用程序Zwitscher中,我已经使用SQLHelper类的方法处理整个db / cursor处理,以便上面的层不必关心它。请参阅其TweetDB课程。

答案 1 :(得分:0)

我解决了这个异常。我在打电话

 db = eventsData.getReadableDatabase();

两次,这就是抛出异常的原因