android – 如何安全地关闭游标和数据库?

前端之家收集整理的这篇文章主要介绍了android – 如何安全地关闭游标和数据库?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用光标从数据库获取了记录.这是完美的工作.但是,当我使用这个代码时,我在Logcat中有一些错误
public Cursor fetchAll()
{
    sqliteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(t1,new String[] {"_id",name,date,prior,time,dateformat},null,prior); 
    return cursor;
}

我的logcat是 –

12-16 14:49:20.774: E/Database(18611): close() was never explicitly called on database '/data/data/com.android.application/databases/appZ.db' 
12-16 14:49:20.774: E/Database(18611): android.database.sqlite.DatabaSEObjectNotClosedException: Application did not close the cursor or database object that was opened here
12-16 14:49:20.774: E/Database(18611):  at android.database.sqlite.sqliteDatabase.<init>(sqliteDatabase.java:1810)
12-16 14:49:20.774: E/Database(18611):  at android.database.sqlite.sqliteDatabase.openDatabase(sqliteDatabase.java:817)
12-16 14:49:20.774: E/Database(18611):  at android.database.sqlite.sqliteDatabase.openOrCreateDatabase(sqliteDatabase.java:851)
12-16 14:49:20.774: E/Database(18611):  at android.database.sqlite.sqliteDatabase.openOrCreateDatabase(sqliteDatabase.java:844)
12-16 14:49:20.774: E/Database(18611):  at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:540)
12-16 14:49:20.774: E/Database(18611):  at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
12-16 14:49:20.774: E/Database(18611):  at android.database.sqlite.sqliteOpenHelper.getWritableDatabase(sqliteOpenHelper.java:98)
12-16 14:49:20.774: E/Database(18611):  at android.database.sqlite.sqliteOpenHelper.getReadableDatabase(sqliteOpenHelper.java:158)
12-16 14:49:20.774: E/Database(18611):  at com.android.todoapplication.DBHelper.fetchAll(DBHelper.java:91)
12-16 14:49:20.774: E/Database(18611):  at com.android.todoapplication.ApplicationActivity.sc_adapter(ApplicationActivity.java:1210)
12-16 14:49:20.774: E/Database(18611):  at com.android.todoapplication.ApplicationActivity.refresh_data(ApplicationActivity.java:1195)
12-16 14:49:20.774: E/Database(18611):  at com.android.todoapplication.ApplicationActivity.onKeyDown(ApplicationActivity.java:1440)
12-16 14:49:20.774: E/Database(18611):  at android.view.KeyEvent.dispatch(KeyEvent.java:1037)
12-16 14:49:20.774: E/Database(18611):  at android.app.Activity.dispatchKeyEvent(Activity.java:2068)
12-16 14:49:20.774: E/Database(18611):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1643)
12-16 14:49:20.774: E/Database(18611):  at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2471)
12-16 14:49:20.774: E/Database(18611):  at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2441)
12-16 14:49:20.774: E/Database(18611):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
12-16 14:49:20.774: E/Database(18611):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-16 14:49:20.774: E/Database(18611):  at android.os.Looper.loop(Looper.java:123)
12-16 14:49:20.774: E/Database(18611):  at android.app.ActivityThread.main(ActivityThread.java:4627)
12-16 14:49:20.774: E/Database(18611):  at java.lang.reflect.Method.invokeNative(Native Method)
12-16 14:49:20.774: E/Database(18611):  at java.lang.reflect.Method.invoke(Method.java:521)
12-16 14:49:20.774: E/Database(18611):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-16 14:49:20.774: E/Database(18611):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-16 14:49:20.774: E/Database(18611):  at dalvik.system.NativeStart.main(Native Method)

我从here提到了一些东西.他们告诉使用getReadableDatabase().我也使用该方法.而且,我也使用这种方法,

public Cursor fetchAll()
{
    sqliteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(t1,prior); 
   if (cursor!=null)
    {
        cursor.close();
    }
   if (db!=null)
    {
        db.close();
    }
    return cursor;
}

之后有错误

Database wasn't open.

之后,我的数据库中也有这个错误.所以任何人都会告诉我如何在获取记录后获取光标和数据库的安全关闭.

解决方法

编辑:如果返回已经关闭的游标,则无法使用.在调用方法之前打开数据库,然后在调用方法关闭游标,然后关闭数据库.

完成后可以关闭光标,如下所示:

Cursor cursor = fetchAll();
... // Do your work with the cursor
cursor.close();

或者如果您使用活动中的游标,并且需要重新排序(活动重新启动,停止…),您可以使用以下命令:

Cursor cursor = fetchAll();
startManagingCursor(cursor); // Android will take care of the cursor for you
// Do your work with the cursor

关闭数据库(关闭游标后,或者最好在onDestroy()中的活动内):

dbhelper.close();

猜你在找的Android相关文章