获取例外,而在Android的访问SQLite数据库 [英] Getting exception while accessing sqlite database in android
本文介绍了获取例外,而在Android的访问SQLite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想我的应用程序访问SQLite数据库并获得此异常:
11月12日至27日:32:12.760:E /异常:(746):java.lang.IllegalStateException:试图收购在ContactListOfNumbersForWhichRuleIsAlreadySpecified发生密切SQLiteClosable异常的引用() DatabaseHandlerRule.java的
我用这code:
公开的ArrayList<串GT; ContactListOfNumbersForWhichRuleIsAlreadySpecified(DatabaseHandlerRule活动)
{
ContactRule接触= NULL;
光标光标= NULL;
SQLiteDatabase DB = NULL;
ArrayList的<串GT; contactList = NULL;
尝试
{
contactList =新的ArrayList<串GT;(); // SQLiteActivity1.ReadingAllContactsRule(SplashActivity.s_dbRule); 字符串selectQuery =SELECT * FROM+ TABLE_CONTACTS +WHERE+ KEY_NAME +!='
+ABCDE#$%&放大器; *()@#$%+'+和+ KEY_PH_NO +=!+ABCDE#$%&放大器; *()@#$%+ +和+ KEY_DATE +='+0+'; DB = this.getReadableDatabase(); 如果(!db.isOpen()){
DB = SQLiteDatabase.openDatabase(
/data/data/com.velosys.smsManager/databases/rulesManager
空,SQLiteDatabase.OPEN_READWRITE);
} 光标= db.rawQuery(selectQuery,NULL); //通过所有行循环,并增加列表
如果(cursor.moveToFirst()){
做{
接触=新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));
//添加联系人名单
contactList.add(contact.getName());
}而(cursor.moveToNext());
}
否则如果(!cursor.moveToFirst())
{
Log.e(消息,第不是在数据库甚至一个单一的数字指定,);
返回contactList;
}
}
赶上(例外五)
{
Log.e(异常:E +异常的ContactListOfNumbersForWhichRuleIsAlreadySpecified()DatabaseHandlerRule.java的发生);
}
最后
{
如果(光标= NULL&放大器;!&安培;!cursor.isClosed())
cursor.close();
如果(DB = NULL&放大器;!&安培; db.isOpen())
db.close();
}
返回contactList;
}
我已经寻找这个异常背后的原因,但没有暗示我的情况:
- 我不是想写,但是读database.Hence没有问题出现在这里对并发性。
- 我使用数据库辅助类的实例
DB = this.getReadableDatabase();
- 我关闭数据库中正确每每次我打开它。
请帮忙me.Thanks提前。
编辑:
其strange.After终于取出并把code,而不finallly块,一切工作fine.Can谁能告诉我这背后的原因是什么?
//最后
{
如果(光标= NULL&放大器;!&安培;!cursor.isClosed())
cursor.close();
如果(DB = NULL&放大器;!&安培; db.isOpen())
db.close();
}
解决方案
请删除finally块。在你有密切的分贝和光标所以这个错误出现。
I am trying to access sqlite database in my app and getting this exception:
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
I am using this code:
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;
}
I have searched for the reasons behind this exception but nothing implies to my case:
- I am not trying to write but read the database.Hence no question arises here about concurrency.
- I am using instance of Database helper class in
db = this.getReadableDatabase();
- I am closing database properly each and everytime i open it.
Please help me.Thanks in advance.
Edit:
Its strange.After removing finally and putting the code without finallly block,everything is working fine.Can anyone please tell me the reason behind this?
//finally
{
if(cursor != null && !cursor.isClosed())
cursor.close();
if(db != null && db.isOpen())
db.close();
}
解决方案
Please remove finally block. in that you have close db and cursor so it this error comes.
这篇关于获取例外,而在Android的访问SQLite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文