我该如何关闭这个光标?

时间:2014-03-01 21:01:42

标签: android sqlite

我是Android开发的新手。我有这个代码,它可以从SQLite数据库中获取一个值。

    EditText TheIDa =(EditText) findViewById(R.id.TheID);

    SQLiteDatabase db;
    db = openOrCreateDatabase( "mySQLite.db", MODE_PRIVATE, null);

    try{

        db=openOrCreateDatabase("mySQLite.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);
    }catch(SQLException e)
    {
        Log.d("Error","Error while Opening Database");
        e.printStackTrace();
    }

    //defining cursor and select all rows from table
    Cursor c=db.rawQuery("SELECT MAX(ID) FROM myTable",null);

    try
    {
        //put cursor on the first position
        c.moveToFirst();

        //fetching all records from cursor until reaching last record
        while(!c.isAfterLast())
        {

            long MyID = c.getLong(0)+1;
            String s = String.valueOf(MyID);
            TheIDa.setText(s);              


            //moving cursor to next record
            c.moveToNext();
        }

        //closing cursor
        c.close();

    }catch(Exception e)
    {
        Log.d("Error","error in cursor");           
        e.printStackTrace();
    }

当我在模拟器中测试代码时,我担心会发出警告 - 我没有关闭光标。我粘贴了下面的logcat输出。我该怎么做才能逃避这个警告?

02-26 19:22:02.501: D/Max ID(25392): ID: 4
02-26 19:22:02.891: D/dalvikvm(25392): GC_FOR_MALLOC freed 4305 objects / 208040 bytes in 57ms
02-26 19:22:02.901: W/SQLiteCompiledSql(25392): Releasing statement in a finalizer. Please ensure that you explicitly call close() on your cursor: SELECT MAX(ID) FROM myTable
02-26 19:22:02.901: W/SQLiteCompiledSql(25392): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
02-26 19:22:02.901: W/SQLiteCompiledSql(25392):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:62)
02-26 19:22:02.901: W/SQLiteCompiledSql(25392):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
02-26 19:22:02.901: W/SQLiteCompiledSql(25392):     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
02-26 19:22:02.901: W/SQLiteCompiledSql(25392):     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
02-26 19:22:02.901: W/SQLiteCompiledSql(25392):     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
02-26 19:22:02.901: W/SQLiteCompiledSql(25392):     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1315)
02-26 19:22:02.901: W/SQLiteCompiledSql(25392):     at org.efbb.efbb.GnXpt.onCreate(GnXpt.java:91)
02-26 19:22:02.901: W/SQLiteCompiledSql(25392):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-26 19:22:02.901: W/SQLiteCompiledSql(25392):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-26 19:22:02.901: W/SQLiteCompiledSql(25392):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-26 19:22:02.901: W/SQLiteCompiledSql(25392):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-26 19:22:02.901: W/SQLiteCompiledSql(25392):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-26 19:22:02.901: W/SQLiteCompiledSql(25392):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 19:22:02.901: W/SQLiteCompiledSql(25392):     at android.os.Looper.loop(Looper.java:123)
02-26 19:22:02.901: W/SQLiteCompiledSql(25392):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-26 19:22:02.901: W/SQLiteCompiledSql(25392):     at java.lang.reflect.Method.invokeNative(Native Method)
02-26 19:22:02.901: W/SQLiteCompiledSql(25392):     at java.lang.reflect.Method.invoke(Method.java:521)
02-26 19:22:02.901: W/SQLiteCompiledSql(25392):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-26 19:22:02.901: W/SQLiteCompiledSql(25392):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-26 19:22:02.901: W/SQLiteCompiledSql(25392):     at dalvik.system.NativeStart.main(Native Method)
02-26 19:22:02.901: E/Database(25392): close() was never explicitly called on database '/data/data/org.efbb.efbb/databases/mySQLite.db' 
02-26 19:22:02.901: E/Database(25392): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
02-26 19:22:02.901: E/Database(25392):  at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1810)
02-26 19:22:02.901: E/Database(25392):  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
02-26 19:22:02.901: E/Database(25392):  at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851)
02-26 19:22:02.901: E/Database(25392):  at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:844)
02-26 19:22:02.901: E/Database(25392):  at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:540)
02-26 19:22:02.901: E/Database(25392):  at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
02-26 19:22:02.901: E/Database(25392):  at org.efbb.efbb.GnXpt.onCreate(GnXpt.java:83)
02-26 19:22:02.901: E/Database(25392):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-26 19:22:02.901: E/Database(25392):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-26 19:22:02.901: E/Database(25392):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-26 19:22:02.901: E/Database(25392):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-26 19:22:02.901: E/Database(25392):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-26 19:22:02.901: E/Database(25392):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 19:22:02.901: E/Database(25392):  at android.os.Looper.loop(Looper.java:123)
02-26 19:22:02.901: E/Database(25392):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-26 19:22:02.901: E/Database(25392):  at java.lang.reflect.Method.invokeNative(Native Method)
02-26 19:22:02.901: E/Database(25392):  at java.lang.reflect.Method.invoke(Method.java:521)
02-26 19:22:02.901: E/Database(25392):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-26 19:22:02.901: E/Database(25392):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-26 19:22:02.901: E/Database(25392):  at dalvik.system.NativeStart.main(Native Method)
02-26 19:22:02.961: E/Database(25392): close() was never explicitly called on database '/data/data/org.efbb.efbb/databases/mySQLite.db' 
02-26 19:22:02.961: E/Database(25392): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
02-26 19:22:02.961: E/Database(25392):  at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1810)
02-26 19:22:02.961: E/Database(25392):  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:817)
02-26 19:22:02.961: E/Database(25392):  at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:851)
02-26 19:22:02.961: E/Database(25392):  at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:844)
02-26 19:22:02.961: E/Database(25392):  at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:540)
02-26 19:22:02.961: E/Database(25392):  at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
02-26 19:22:02.961: E/Database(25392):  at org.efbb.efbb.GnXpt.onCreate(GnXpt.java:79)
02-26 19:22:02.961: E/Database(25392):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-26 19:22:02.961: E/Database(25392):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-26 19:22:02.961: E/Database(25392):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-26 19:22:02.961: E/Database(25392):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-26 19:22:02.961: E/Database(25392):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-26 19:22:02.961: E/Database(25392):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 19:22:02.961: E/Database(25392):  at android.os.Looper.loop(Looper.java:123)
02-26 19:22:02.961: E/Database(25392):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-26 19:22:02.961: E/Database(25392):  at java.lang.reflect.Method.invokeNative(Native Method)
02-26 19:22:02.961: E/Database(25392):  at java.lang.reflect.Method.invoke(Method.java:521)
02-26 19:22:02.961: E/Database(25392):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-26 19:22:02.961: E/Database(25392):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-26 19:22:02.961: E/Database(25392):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

我不明白为什么你会得到这个例外,但尝试这样的事情:

try
{
    //put cursor on the first position
    c.moveToFirst();

    //fetching all records from cursor until reaching last record
    while(!c.isAfterLast())
    {

        long MyID = c.getLong(0)+1;
        String s = String.valueOf(MyID);
        TheIDa.setText(s);              


        //moving cursor to next record
        c.moveToNext();
    }

} catch(Exception e) {
    Log.d("Error","error in cursor");           
    e.printStackTrace();
} finally {
    //closing cursor
    c.close();
}

答案 1 :(得分:0)

我认为你也需要关闭你的数据库:)使用:db.close();