访问sqlite数据库时,由于未处理的语句异常而无法关闭

时间:2012-12-30 06:38:21

标签: android sqlite exception

我收到以下异常:在我的Android应用程序中访问SQLiteDatabase时,由于未定义的语句而无法关闭。这是我正在使用的代码:

public ArrayList<String> getTheNumbersAssociatedWithFolder(String FileName,String FileAddress) 
{
    ContactRule contact = null;
    ArrayList<String> contactList = null;
    Cursor cursor = null;
    SQLiteDatabase db = null;
    try 
    {
        contactList = new ArrayList<String>();


        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS + " WHERE "+KEY_FolderAddress+" ='"
        +FileAddress+"'"+" AND "+KEY_FOLDER_NAME+" ='"+FileName+"'"+" AND "+KEY_DATE+" ='"+"0"+"'"
        +" AND "+KEY_NAME+" !='"+"abcde#$%&*@#$%"+"'"+" AND "+KEY_PH_NO+" !='"+"abcde#$%&*@#$%"+"'";


        db = this.getReadableDatabase();

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

        cursor = db.rawQuery(selectQuery, null);

        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
                if(contact.getName().length() != 0)
                    contactList.add(contact.getName() );
            } while (cursor.moveToNext());
        }
        else
        {
            Log.e("Message: ","There is no such Contact present in getTheNumbersAssociatedWithFolder(String FileName,String FileAddress) in DatabaseHandlerRule.java ");                
        }
        if(cursor != null && !cursor.isClosed())
            cursor.close(); 
        if(db != null)
            db.close();
    }
    catch(Exception e)
    {
        Log.e("Exception: ",e+" Exception occured in getTheNumbersAssociatedWithFolder() of DatabaseHandlerRule.java");
        if(cursor != null && !cursor.isClosed())
            cursor.close(); 
        if(db != null)
            db.close();
    }
    return contactList;
}

我研究了很多关于这个例外的事情,并且知道这个例外背后有两个主要原因:

1. You are not closing the cursor after accessing the database.
2. You are closing the database first and then cursor.

在我的代码中,这两个条件都没有实现。我想告诉我更多的事情我在后台线程中调用这段代码。但我认为这不是问题因为我是只读数据库而不是写它。

0 个答案:

没有答案
相关问题