2012-12-27 31 views
0

訪問SQLite數據庫我想在我的應用程序訪問SQLite數據庫並獲得此異常:獲取例外,而在Android的

12-27 11:32:12.760: E/Exception:(746): java.lang.IllegalStateException: attempt to acquire a reference on a close SQLiteClosable Exception occured in ContactListOfNumbersForWhichRuleIsAlreadySpecified() of DatabaseHandlerRule.java 

我使用這個代碼:

public ArrayList<String> ContactListOfNumbersForWhichRuleIsAlreadySpecified(DatabaseHandlerRule Activity) 
    { 
     ContactRule contact = null; 
     Cursor cursor = null; 
     SQLiteDatabase db = null; 
     ArrayList<String> contactList = null; 
     try 
     { 
      contactList = new ArrayList<String>(); 

      //  SQLiteActivity1.ReadingAllContactsRule(SplashActivity.s_dbRule); 

      String selectQuery = "SELECT * FROM " + TABLE_CONTACTS + " WHERE "+KEY_NAME+" !='" 
      +"abcde#$%&*()@#$%"+"'"+" AND "+KEY_PH_NO+"!='"+"abcde#$%&*()@#$%"+"'"+" AND "+KEY_DATE+" ='"+"0"+"'"; 

      db = this.getReadableDatabase(); 

      if (!db.isOpen()) { 
       db = SQLiteDatabase.openDatabase(
         "/data/data/com.velosys.smsManager/databases/rulesManager", 
         null, SQLiteDatabase.OPEN_READWRITE); 
      } 

      cursor = db.rawQuery(selectQuery, null); 

      // looping through all rows and adding to list 
      if (cursor.moveToFirst()) { 
       do { 
        contact = new ContactRule(); 
        contact.setID(Integer.parseInt(cursor.getString(0))); 
        contact.setName(cursor.getString(1)); 
        contact.setPhoneNumber(cursor.getString(2)); 
        contact.setFolderName(cursor.getString(3)); 
        contact.setParentFolderAddress(cursor.getString(4)); 
        contact.setTime(cursor.getLong(5)); 
        contact.setDate(cursor.getLong(6)); 
        // Adding contact to list 
        contactList.add(contact.getName()); 
       } while (cursor.moveToNext()); 
      } 
      else if(!cursor.moveToFirst()) 
      { 
       Log.e("Message: ","Rule is not specified for even a single number in database"); 
       return contactList;    
      } 
     } 
     catch(Exception e) 
     { 
      Log.e("Exception: ",e+" Exception occured in ContactListOfNumbersForWhichRuleIsAlreadySpecified() of DatabaseHandlerRule.java"); 
     } 
     finally 
     { 
      if(cursor != null && !cursor.isClosed()) 
       cursor.close(); 
      if(db != null && db.isOpen()) 
       db.close(); 
     } 
     return contactList; 
    } 

...我已經尋找這個例外背後的原因,但沒有暗示我的情況:

  1. 我不想寫但讀取數據庫。因此這裏沒有關於併發性的問題。
  2. 我正在使用的數據庫幫助類的實例db = this.getReadableDatabase();
  3. 我正在關閉數據庫正確每次我打開它。

請幫助我。預先感謝。

編輯:

其strange.After終於卸下,並把該代碼而不finallly塊,一切工作fine.Can誰能告訴我這背後的原因是什麼?

  //finally 
      { 
       if(cursor != null && !cursor.isClosed()) 
        cursor.close(); 
       if(db != null && db.isOpen()) 
        db.close(); 
      } 
+0

哪一行導致異常? – Squonk

+0

你能告訴你在哪一行得到這個錯誤嗎? –

+0

我不能說,因爲在調試時,我無法產生錯誤。在調試時一切正常。 – user1726619

回答

3

請刪除finally塊。因爲你有接近的數據庫和光標,所以這個錯誤來了。

+0

我應該在完成我的工作後關閉遊標和數據庫。是不是?如果我在finally子句中關閉它們,那麼問題是什麼?您能告訴我嗎? – user1726619

+0

因爲你有相同的數據庫和光標在文件DatabaseHandlerRule.java –

+0

@VatsalShah請檢查編輯,並請說明它背後的原因。 – user1726619

0

在聲明db = this.getReadableDatabase();中,我不確定您使用的是什麼上下文。所以,像聲明一個Context context = this;全球範圍內,然後調用最終除去,並把該代碼而不finallly阻止,一切工作fine.Can誰能告訴我這背後的原因,聲明作爲db = context.getReadableDatabase();

+0

使用此我使用數據庫打開幫助器類即DatabaseHandlerRule的上下文。 this = DatabaseHandlerRule.this; – user1726619

+0

我認爲我建議的方法更直接。請嘗試一下並說出口。因爲我不熟悉你的參考方法:( –

0

其strange.After?

//finally 
      { 
       if(cursor != null && !cursor.isClosed()) 
        cursor.close(); 
       if(db != null && db.isOpen()) 
        db.close(); 
      } 
相關問題