訪問SQLite數據庫我想在我的應用程序訪問SQLite數據庫並獲得此異常:獲取例外,而在Android的
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();
- 我正在關閉數據庫正確每次我打開它。
請幫助我。預先感謝。
編輯:
其strange.After終於卸下,並把該代碼而不finallly塊,一切工作fine.Can誰能告訴我這背後的原因是什麼?
//finally
{
if(cursor != null && !cursor.isClosed())
cursor.close();
if(db != null && db.isOpen())
db.close();
}
哪一行導致異常? – Squonk
你能告訴你在哪一行得到這個錯誤嗎? –
我不能說,因爲在調試時,我無法產生錯誤。在調試時一切正常。 – user1726619