我创建了这个方法来了解我的 Sqlite 数据库中是否存在联系人。
public Cursor doesContactExist(String name)
{
return db.rawQuery("select * from contacts where name = ?; ", new String[] {name});
}
但是无论何时从另一个 Activity 中调用它,它只会在此时崩溃....
if(db.doesContactExist(name)==null){ <== crashes here
try {
db.open();
db.insertContact(name,cNumber);//name and number are Strings
listItems.add(name); //a List View array
db.close();
adapter.notifyDataSetChanged(); //adapter for ListView
}
catch (Exception e) {
//some code }
}
根据我的说法,rawQuery 有一些问题,尤其是其中的 sql 字符串。
有什么帮助吗?
最佳答案
您尝试在数据库打开之前进行选择:
if(db.doesContactExist(name)==null){ <== it is closed here
try {
db.open(); <== open the db here
将其更改为:
db.open();
if(db.doesContactExist(name)==null){
try {
关于Android SQLite rawQuery 带来麻烦,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15549738/