2

在某些情况下,当我尝试使用以下代码打开我的 sqlite 数据库时:

  dbManager.open();

其中方法如下所示:

  SQLiteDatabase mDb;
  DbHelper mDbHelper;

   public void open(){  
        mDb=mDbHelper.getWritableDatabase();
   }

我收到此错误:

11-08 12:39:25.760: E/SQLiteDatabase(5603): close() was never explicitly called on database '/data/data/act.myactivity/databases/seasonDb' 
11-08 12:39:25.760: E/SQLiteDatabase(5603): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
11-08 12:39:25.760: E/SQLiteDatabase(5603):     at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:1980)
11-08 12:39:25.760: E/SQLiteDatabase(5603):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:977)
11-08 12:39:25.760: E/SQLiteDatabase(5603):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:956)
11-08 12:39:25.760: E/SQLiteDatabase(5603):     at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1021)
11-08 12:39:25.760: E/SQLiteDatabase(5603):     at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:753)
11-08 12:39:25.760: E/SQLiteDatabase(5603):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:221)
11-08 12:39:25.760: E/SQLiteDatabase(5603):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:149)
11-08 12:39:25.760: E/SQLiteDatabase(5603):     at host.database.DB_DatabaseManager.open(DB_DatabaseManager.java:136)
11-08 12:39:25.760: E/SQLiteDatabase(5603):     at host.framework.ServicePromemoria.checkUnSyncInsert(ServicePromemoria.java:265)
11-08 12:39:25.760: E/SQLiteDatabase(5603):     at host.framework.ServicePromemoria$1.run(ServicePromemoria.java:59)
11-08 12:39:25.760: E/SQLiteDatabase(5603):     at java.util.Timer$TimerImpl.run(Timer.java:284)

我认为这与尝试打开尚未关闭的数据库有关。

ps:关闭数据库连接的方法是:

 public void close(){
        mDb.close();
}

编辑:

这是核心checkUnSyncInsert()

  dbManager.open(); //here error occurs
    String getUserQuery = "SELECT * FROM user";
    Cursor c = dbManager.RawQuery(getUserQuery, null);

    if(c.getCount() != 0)
    {
        try{
            c.moveToFirst();
            usr = c.getString(0).toString();
            pwd = c.getString(1).toString();
            Log.i("USERNAME: "+pwd,"PASSWORD: "+pwd);
            c.close();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            c.close();
        }
    }
    else

    dbManager.close();

错误发生在第一行 ( dbManager.open())

4

3 回答 3

0

只有在没有返回任何内容时才会关闭。如果这段代码运行了几次,那么报错是可以理解的,因为第二次关闭时在打开时丢失了

于 2012-11-08T12:21:26.733 回答
0

首先确保您正在关闭数据库,然后再尝试再次打开它。你甚至可以在你的方法中打开和关闭数据库:

public void addGroupContact(String groupId, String contactId) {

        Log.d("Database : ", "*****************Ädd Group Contact******************");
        mDbHelper = new NewDatabaseHelper();
        mDatabase = mDbHelper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(NewDatabaseHelper.KEY_GROUP_F_ID, groupId); // Group Id
        values.put(NewDatabaseHelper.KEY_CONTACT_ID, contactId); // contact id
        // Inserting Row
        mDatabase.insert(NewDatabaseHelper.TABLE_GROUPS_CONTACTS, null, values);
        mDatabase.close(); // Closing database connection
    }
于 2012-11-09T13:01:07.820 回答
-1

你必须确保数据库在销毁和暂停时关闭

于 2012-11-08T12:27:15.227 回答