从未在android中的数据库上显式调用Close()

时间:2012-01-19 08:35:37

标签: android database sqlite

我在DatabaseHelper.java上尝试了这段代码

第34行

public DatabaseHelper(Context context) throws IOException {
    super(context, DB_NAME, null, 1);
    this.mycontext = context;
    boolean dbexist = checkdatabase();
    if (dbexist) {
        // System.out.println("Database exists");
        opendatabase();
    } else {
        // System.out.println("Database doesn't exist");
        createdatabase();
    }
}

第64行

private boolean checkdatabase() {
    // SQLiteDatabase checkdb = null;
    boolean checkdb = false;
    try {
        String myPath = DB_PATH + DB_NAME;
        File dbfile = new File(myPath);
        checkdb = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READWRITE) != null;
        checkdb = dbfile.exists();
    } catch (SQLiteException e) {
        System.out.println("Database doesn't exist");
    }

    return checkdb;
}

第44行

在创建方法menu.java

try {
        db = new DatabaseHelper(this);
    } catch (IOException e2) {

        e2.printStackTrace();
    }

    try {
        db.createdatabase();
    } catch (IOException e) {

        e.printStackTrace();
    }
     db.getReadableDatabase();

    db.opendatabase();
    Random rand = new Random();
    randomJokId = rand.nextInt(603 - 1) + 1;
    cur = db.jokOfTheDay(randomJokId);
    cur.moveToFirst();
    String fullJok = cur.getString(cur.getColumnIndex("body"));
    String Jok = "";
    Jok = fullJok.substring(0, 55);
    jok_of_the_day.setText(Jok + "...  Read more");

    Log.d(TAG, "" + randomJokId);
}

收到此错误消息

另一件重要的事情,我有这个错误信息三次

01-19 13:49:41.284: ERROR/Database(10932): close() was never explicitly called on database '/data/data/com.horror.android/databases/jokesdatabase.sql' 
01-19 13:49:41.284: ERROR/Database(10932): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
01-19 13:49:41.284: ERROR/Database(10932):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1847)
01-19 13:49:41.284: ERROR/Database(10932):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:820)
01-19 13:49:41.284: ERROR/Database(10932):     at com.horror.android.DatabaseHelper.checkdatabase(DatabaseHelper.java:64)
01-19 13:49:41.284: ERROR/Database(10932):     at com.horror.android.DatabaseHelper.createdatabase(DatabaseHelper.java:45)
01-19 13:49:41.284: ERROR/Database(10932):     at com.horror.android.UltimateJokesMenu.onCreate(UltimateJokesMenu.java:51)
01-19 13:49:41.284: ERROR/Database(10932):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-19 13:49:41.284: ERROR/Database(10932):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-19 13:49:41.284: ERROR/Database(10932):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-19 13:49:41.284: ERROR/Database(10932):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-19 13:49:41.284: ERROR/Database(10932):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-19 13:49:41.284: ERROR/Database(10932):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 13:49:41.284: ERROR/Database(10932):     at android.os.Looper.loop(Looper.java:130)
01-19 13:49:41.284: ERROR/Database(10932):     at android.app.ActivityThread.main(ActivityThread.java:3683)
01-19 13:49:41.284: ERROR/Database(10932):     at java.lang.reflect.Method.invokeNative(Native Method)
01-19 13:49:41.284: ERROR/Database(10932):     at java.lang.reflect.Method.invoke(Method.java:507)
01-19 13:49:41.284: ERROR/Database(10932):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-19 13:49:41.284: ERROR/Database(10932):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-19 13:49:41.284: ERROR/Database(10932):     at dalvik.system.NativeStart.main(Native Method)

5 个答案:

答案 0 :(得分:1)

当您的数据库使用结束时,更好的方法是使用SQLiteDatabase.close()。关闭 光标也cursor.close();

对于游标使用startmanagincursor(cursor)。只需给它赋值一些

请记住,如果你不关闭数据库,那么它可能会产生严重的问题

答案 1 :(得分:0)

尝试在SQLiteDatabase.close()方法的try末尾调用checkDatabase

答案 2 :(得分:0)

使用db.close();关闭数据库,cur.close();使用它们后关闭光标。

答案 3 :(得分:0)

使用onStop()方法关闭数据库并使用startmanagingcursor()和所有游标自动处理其生命周期。

答案 4 :(得分:0)

onPause中的onStop关闭就足够startManagingCursorstartManagingCursor。使用{{1}}除了被弃用之外,它可能会带来处理更新视图的问题。我看到要评估的两个问题:

  • 关闭数据库。足以遵循这里给出的答案activities lifecycle,即:

      从字面上理解数据库通常不是关闭的,实际的   因为重复实例化你的数据库,或连接你已经   设置,你并尝试打开另一个连接将没有   例外。所以解决方案是,确保你只打开一个   连接。

    简单来说:每次打开数据库连接都会关闭它。这可以通过确保只有一个连接来实现,例如,您可以使用方法Android error - close() was never explicitly called on database或确保每次打开它时都关闭。

  • 正如我在使用{{1}}之前所说的那样关闭游标并不是一个好主意。相反,您应该使用支持库并使用CursorLoader方法,它涉及使用ContentProvider,但如果您认为是一种矫枉过正,您可能会阅读isOpen