我在使用 SQLite 时遇到了最常见的错误。不幸的是,我找不到我的问题的答案,因为发生此错误时,我没有使用游标。插入+更新后出现此错误。有错误文字:
09-16 08:03:33.253: E/Cursor(1436): Finalizing a Cursor that has not been deactivated or closed. database = /data/data/*******/databases/settings.db, table = null, query = select * from preferences where pref_name = 'preference'
09-16 08:03:33.253: E/Cursor(1436): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
09-16 08:03:33.253: E/Cursor(1436): at android.database.sqlite.SQLiteCursor.<init>(SQLiteCursor.java:210)
09-16 08:03:33.253: E/Cursor(1436): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:53)
09-16 08:03:33.253: E/Cursor(1436): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
09-16 08:03:33.253: E/Cursor(1436): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1315)
09-16 08:03:33.253: E/Cursor(1436): at ****.db.SettingsSQLiteHelper.updateValue(SettingsSQLiteHelper.java:132)
09-16 08:03:33.253: E/Cursor(1436): at ****.PreferencesActivity$1.onClick(PreferencesActivity.java:67)
09-16 08:03:33.253: E/Cursor(1436): at android.view.View.performClick(View.java:2408)
09-16 08:03:33.253: E/Cursor(1436): at android.view.View$PerformClick.run(View.java:8816)
09-16 08:03:33.253: E/Cursor(1436): at android.os.Handler.handleCallback(Handler.java:587)
09-16 08:03:33.253: E/Cursor(1436): at android.os.Handler.dispatchMessage(Handler.java:92)
09-16 08:03:33.253: E/Cursor(1436): at android.os.Looper.loop(Looper.java:123)
09-16 08:03:33.253: E/Cursor(1436): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-16 08:03:33.253: E/Cursor(1436): at java.lang.reflect.Method.invokeNative(Native Method)
09-16 08:03:33.253: E/Cursor(1436): at java.lang.reflect.Method.invoke(Method.java:521)
09-16 08:03:33.253: E/Cursor(1436): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-16 08:03:33.253: E/Cursor(1436): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-16 08:03:33.253: E/Cursor(1436): at dalvik.system.NativeStart.main(Native Method)
有代码,在执行之后,当应用程序尝试执行任何 sql 时,我收到错误,如上所述:
public boolean updateValue(String property, String value) {
SQLiteDatabase database = getWritableDatabase();
try {
ContentValues cv = new ContentValues();
cv.put(COLUMN_PREFNAME, property);
cv.put(COLUMN_PREFVALUE, value);
if (database.rawQuery(
"select * from " + TABLE_PREFERENCES + " where "
+ COLUMN_PREFNAME + " = '" + property + "'", null)
.getCount() < 1) {
Log.i("MyLog", "There wasn't property " + property
+ " and system inserted new row");
database.insert(TABLE_PREFERENCES, null, cv);
} else {
cv.clear();
cv.put(COLUMN_PREFVALUE, value);
database.update(TABLE_PREFERENCES, cv, COLUMN_PREFNAME
+ "='" + property + "'", null);
}
database.close();
} catch (Exception e) {
Log.e("MyLog", "Error in SettingsDB.updateValue :: " + e);
database.close();
return false;
}
return true;
}
综上所述,应用程序的首次启动执行上面描述的方法来设置应用程序的偏好。使用此方法后,应用程序由于错误而无法运行任何 SQL。当我重新启动应用程序时,它运行良好,因为应用程序不会再次启动该方法。我相信有某种看不见的光标,我没有关闭它。您能否建议我,我应该添加或关闭什么来消除此错误?谢谢!