2011-01-19 27 views
2

我們如何避免這種異常?如何避免DatabaseObjectNotClosedException

 
01-19 17:16:16.024: WARN/SQLiteCompiledSql(477): Releasing statement in a finalizer. Please ensure that you explicitly call close() on your cursor: SELECT * FROM test 

01-19 17:16:16.024: WARN/SQLiteCompiledSql(477): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here 

01-19 17:16:16.024: WARN/SQLiteCompiledSql(477): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here 

01-19 17:16:16.024: WARN/SQLiteCompiledSql(477):  at android.database.sqlite.SQLiteCompiledSql.(SQLiteCompiledSql.java:62) 

01-19 17:16:16.024: WARN/SQLiteCompiledSql(477):  at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:80) 

01-19 17:16:16.024: WARN/SQLiteCompiledSql(477):  at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:46) 

回答

5

我覺得這個錯誤是因爲你沒有關閉你的數據庫。始終關閉sqlitehelper類

編輯:

您必須實現這樣在你的類,這是擴展SQLiteOpenHelper類:上

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

其中i關閉cursor..after所有的數據庫操作。或者在OnDestroy..pls幫助 – jennifer 2011-01-21 05:05:38

3

好,因爲它說,調用close()你的光標!

1

我也得到了異常,當我有一個錯誤的SQL查詢使用遊標時。這聽起來很愚蠢,但如果有人發現同樣的,它的另一個原因......

0

我解決了異常。我打電話

db = eventsData.getReadableDatabase(); 

兩次這就是爲什麼拋出異常