gpt4 book ai didi

java - android&/sqlite : cursor causes problems

转载 作者:行者123 更新时间:2023-11-29 03:48:41 25 4
gpt4 key购买 nike

我有一个与 sqlite 相关的游标的错误。我将 SQLiteOpenHelper 子类化以访问数据库。然后,我访问数据库并随后直接关闭光标。尽管如此,当我关闭 Activity 时,这会导致错误:

代码如下所示:

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)

有人知道是什么原因造成的吗?

最佳答案

像这样重写你的代码:

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();

否则,您会连接到您从未关闭的数据库,这会导致问题。

关于java - android&/sqlite : cursor causes problems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9704061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com