アプリで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;
}
私はこの例外の背後にある理由を検索しましたが、私の場合には何も意味しません。
- 私はデータベースを書き込もうとはしていませんが、読み取ろうとしているので、並行性についてここで疑問は生じません。
- でデータベースヘルパークラスのインスタンスを使用しています
db = this.getReadableDatabase();
- データベースを開くたびに、データベースを適切に閉じています。
助けてください。よろしくお願いします。
編集:
最後に削除し、最後にブロックせずにコードを配置した後、すべてが正常に機能しています。この背後にある理由を教えてもらえますか?
//finally
{
if(cursor != null && !cursor.isClosed())
cursor.close();
if(db != null && db.isOpen())
db.close();
}