在android中访问sqlite数据库时获取异常

时间:2012-12-27 06:09:05

标签: android sqlite exception

我正在尝试访问我的应用程序中的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. 中使用了数据库帮助程序类的实例
  4. 每次打开数据库时,我都会正确关闭数据库。
  5. 请帮帮我。谢谢。

    编辑:

    它很奇怪。最后删除并且没有完全阻止代码,一切都运行正常。任何人都可以告诉我这背后的原因?

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

3 个答案:

答案 0 :(得分:3)

请删除finally块。因为你有关闭数据库和光标所以它出现了这个错误。

答案 1 :(得分:0)

db = this.getReadableDatabase();语句中,我不确定您使用的是什么上下文。因此,声明像Context context = this;这样的全局上下文,然后将该语句称为db = context.getReadableDatabase();

答案 2 :(得分:0)

它很奇怪。最后删除并且没有最终阻止代码,一切都运行正常。任何人都可以告诉我这背后的原因?

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