0

我在使用 android 的 Sqlite 时遇到问题。日志告诉我:

SQLiteConnectionPool(1730): **The connection pool for /data/data/dev.xx.xx/databases/XX has been closed but there are still 1 connections in use.  They will be closed as they are released back to the pool.**
08-02 14:15:41.490: D/AndroidRuntime(1730): Shutting down VM
08-02 14:15:41.490: W/dalvikvm(1730): threadid=1: thread exiting with uncaught exception (group=0xb4d74908)
08-02 14:15:41.510: E/AndroidRuntime(1730): FATAL EXCEPTION: main
08-02 14:15:41.510: E/AndroidRuntime(1730): **java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/dev.xx.xx/databases/XX**
08-02 14:15:41.510: E/AndroidRuntime(1730):     at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
08-02 14:15:41.510: E/AndroidRuntime(1730):     at android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:520)
08-02 14:15:41.510: E/AndroidRuntime(1730):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:263)
08-02 14:15:41.510: E/AndroidRuntime(1730):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
08-02 14:15:41.510: E/AndroidRuntime(1730):     at dev.xx.xx.SettingActivity$3.onClick(SettingActivity.java:205)
08-02 14:15:41.510: E/AndroidRuntime(1730):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
08-02 14:15:41.510: E/AndroidRuntime(1730):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 14:15:41.510: E/AndroidRuntime(1730):     at android.os.Looper.loop(Looper.java:137)
08-02 14:15:41.510: E/AndroidRuntime(1730):     at android.app.ActivityThread.main(ActivityThread.java:5039)
08-02 14:15:41.510: E/AndroidRuntime(1730):     at java.lang.reflect.Method.invokeNative(Native Method)
08-02 14:15:41.510: E/AndroidRuntime(1730):     at java.lang.reflect.Method.invoke(Method.java:511)
08-02 14:15:41.510: E/AndroidRuntime(1730):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-02 14:15:41.510: E/AndroidRuntime(1730):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

当我尝试调用时出现此错误:

public class DataBase extends SQLiteOpenHelper {

...
    void addUser(String who, String time, String table, String phone) {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(KEY_NAME, who);
            values.put(KEY_PHONE, phone);
            values.put(KEY_TABLE_RES, table);
            values.put(KEY_TIME, time);
            db.insert(TABLE_RESERVATION, null, values);
            db.close(); // Closing database connection
        }

....
}

我认为这是 func 调用的时候:

this.getWritableDatabase()

4

1 回答 1

2

我不相信你应该每次都得到一个新的 SQLiteDatabase 实例。您希望在 SQLiteOpenHelper 中保留对 SQLiteDatabase 对象的引用。您的 SQLiteOpenHelper 应该构造类似于以下内容:

public class DataBase extends SQLiteOpenHelper {

...
    private SQLiteDatabase mDatabase;

    @Override
    public void onCreate(SQLiteDatabase db) {
        ...
        mDatabase = db;
        ...
    }

    void addUser(String who, String time, String table, String phone) {
        ContentValues values = new ContentValues();
        values.put(KEY_NAME, who);
        values.put(KEY_PHONE, phone);
        values.put(KEY_TABLE_RES, table);
        values.put(KEY_TIME, time);
        mDatabase.insert(TABLE_RESERVATION, null, values);
    }

....
}
于 2013-08-02T18:36:11.217 回答