在Android中发出打开sqlite数据库连接

时间:2012-11-08 11:54:50

标签: android database sqlite database-connection

在某些情况下,当我尝试使用follownig代码打开我的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)

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

p.s。:关闭数据库连接的方法是:

 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()

3 个答案:

答案 0 :(得分:0)

只有在没有任何回复的情况下才会关闭。如果这段代码运行了几次,那么错误是可以理解的,因为第二次打开时缺少关闭

答案 1 :(得分: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
    }

答案 2 :(得分:-1)

你必须确保在Destroy and Pause

上关闭数据库