0

I am trying to access sqlite database in my app and getting this exception:

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

I am using this code:

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;
    }

I have searched for the reasons behind this exception but nothing implies to my case:

  1. I am not trying to write but read the database.Hence no question arises here about concurrency.
  2. I am using instance of Database helper class in db = this.getReadableDatabase();
  3. I am closing database properly each and everytime i open it.

Please help me.Thanks in advance.

Edit:

Its strange.After removing finally and putting the code without finallly block,everything is working fine.Can anyone please tell me the reason behind this?

          //finally
            {   
                if(cursor != null && !cursor.isClosed())
                    cursor.close(); 
                if(db != null && db.isOpen())
                    db.close();
            }
user1726619
  • 941
  • 2
  • 18
  • 39

3 Answers3

3

Please remove finally block. in that you have close db and cursor so it this error comes.

Vatsal Shah
  • 42
  • 1
  • 6
0

In the statement db = this.getReadableDatabase();, I'm not sure of what context you are using. So declare a global context like Context context = this; then call that statement as db = context.getReadableDatabase();

Manoj Kumar
  • 1,510
  • 3
  • 20
  • 40
  • Using this i am using the context of database open helper class i.e DatabaseHandlerRule. this = DatabaseHandlerRule.this; – user1726619 Dec 27 '12 at 06:22
  • I think the method I suggested is more direct. Please try it out and say. Because I'm not familiar with your method of referencing :( – Manoj Kumar Dec 27 '12 at 06:25
0

Its strange.After removing finally and putting the code without finallly block,everything is working fine.Can anyone please tell me the reason behind this?

  //finally
            {   
                if(cursor != null && !cursor.isClosed())
                    cursor.close(); 
                if(db != null && db.isOpen())
                    db.close();
            }
user1726619
  • 941
  • 2
  • 18
  • 39