2012-03-14 68 views
1

我有一個與sqlite相關的遊標有此錯誤。我的SQLiteOpenHelper子類訪問數據庫。然後,我訪問數據庫,然後直接關閉我的光標。不過,這將導致一個錯誤,當我關閉活動:安卓&sqlite:遊標導致問題

的代碼看起來是這樣的:

String sql = "SELECT * FROM table_bla LIMIT 1"; 
Cursor cursor = dbHelper.getWritableDatabase().rawQuery(sql, null); 

cursor.moveToFirst(); 
String x = cursor.getString(cursor.getColumnIndex("x")); 
cursor.close(); 

而且我得到這個:

03-14 15:34:55.945: E/Database(3825): close() was never explicitly called on database '/data/data/my.project/databases/myDB.db' 
03-14 15:34:55.945: E/Database(3825): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here 
03-14 15:34:55.945: E/Database(3825): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1847) 
03-14 15:34:55.945: E/Database(3825): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820) 
03-14 15:34:55.945: E/Database(3825): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:854) 
03-14 15:34:55.945: E/Database(3825): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:847) 
03-14 15:34:55.945: E/Database(3825): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:572) 
03-14 15:34:55.945: E/Database(3825): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203) 
03-14 15:34:55.945: E/Database(3825): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118) 
03-14 15:34:55.945: E/Database(3825): at my.project.StartScreen.onResume(StartScreen.java:35) 
03-14 15:34:55.945: E/Database(3825): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150) 
03-14 15:34:55.945: E/Database(3825): at android.app.Activity.performResume(Activity.java:3832) 
03-14 15:34:55.945: E/Database(3825): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2231) 
03-14 15:34:55.945: E/Database(3825): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2256) 
03-14 15:34:55.945: E/Database(3825): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1789) 
03-14 15:34:55.945: E/Database(3825): at android.app.ActivityThread.access$1500(ActivityThread.java:123) 
03-14 15:34:55.945: E/Database(3825): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939) 
03-14 15:34:55.945: E/Database(3825): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-14 15:34:55.945: E/Database(3825): at android.os.Looper.loop(Looper.java:130) 
03-14 15:34:55.945: E/Database(3825): at android.app.ActivityThread.main(ActivityThread.java:3835) 
03-14 15:34:55.945: E/Database(3825): at java.lang.reflect.Method.invokeNative(Native Method) 
03-14 15:34:55.945: E/Database(3825): at java.lang.reflect.Method.invoke(Method.java:507) 
03-14 15:34:55.945: E/Database(3825): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847) 
03-14 15:34:55.945: E/Database(3825): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 
03-14 15:34:55.945: E/Database(3825): at dalvik.system.NativeStart.main(Native Method) 

的人都知道是什麼原因導致的?

+1

你關閉了數據庫對象嗎? – 2012-03-14 14:40:26

回答

2

重寫代碼那樣:

String sql = "SELECT * FROM table_bla LIMIT 1"; 
SQLiteDatabase database = dbHelper.getWritableDatabase(); 
Cursor cursor = database.rawQuery(sql, null); 

cursor.moveToFirst(); 
String x = cursor.getString(cursor.getColumnIndex("x")); 
cursor.close(); 
database.close(); 

否則你到數據庫你從來沒有關閉,這將導致該問題的連接。

+0

此外,他應該得到一個可讀的數據庫而不是可寫的數據庫,因爲他沒有對數據庫進行任何修改。 – dymmeh 2012-03-14 14:42:36

+0

並不重要 - 如果可以,'getReadableDatabase()'會給你一個可寫的。編輯:以及要求一個可讀的數據庫更安全,因爲它試圖更難得到一個數據庫。 – zapl 2012-03-14 14:46:39

2

這是因爲您需要在退出應用程序之前關閉對數據庫的訪問權限,我認爲這段代碼並沒有強制關閉。