0

我正在尝试在我的应用程序中访问 sqlite 数据库并收到此异常:

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. 每次打开数据库时,我都会正确关闭数据库。

请帮助我。在此先感谢。

编辑:

很奇怪。在删除 finally 并放置没有 finally 块的代码后,一切正常。谁能告诉我这背后的原因?

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

3 回答 3

3

请删除 finally 块。因为你有关闭的数据库和游标所以这个错误来了。

于 2012-12-27T06:17:43.037 回答
0

很奇怪。在删除 finally 并放置没有 finally 块的代码后,一切正常。谁能告诉我这背后的原因?

  //finally
            {   
                if(cursor != null && !cursor.isClosed())
                    cursor.close(); 
                if(db != null && db.isOpen())
                    db.close();
            }
于 2012-12-27T06:41:25.563 回答
0

在声明中db = this.getReadableDatabase();,我不确定您使用的是什么上下文。所以声明一个全局上下文,Context context = this;然后将该语句称为db = context.getReadableDatabase();

于 2012-12-27T06:19:10.713 回答