[英]Android Sqlite3 database: Cursor in SQLite is not returning null?
Could someone advise me why I am only getting true
result, even though that record does not exist in my database? 有人可以告诉我为什么即使我的数据库中不存在该记录,我还是只能得到true
结果吗?
For clarification, in my table, _id
is the telephone number. 为了澄清起见,在我的表格中, _id
是电话号码。
Here's my code: 这是我的代码:
public boolean checkifExists(String number){
String[] columns = new String[]{"_id"};
String[] wehreargs = new String[]{number};
this.openDataBase();
if (myDataBase.query("mytable", columns, "_id=?", wehreargs, null, null, null)==null){
myDataBase.query("mytable", columns, "_id=?", wehreargs, null, null, null).close();
this.close();
return false;
} else {
myDataBase.query("mytable", columns, "_id=?", wehreargs, null, null, null).close();
this.close();
return true;
}
}
As @DougCurrie mentions: Query is returning non-Null cursors only: revised your code: 正如@DougCurrie所提到的:查询仅返回非null游标:修改了代码:
public boolean checkifExists(String number){
String[] columns = new String[]{"_id"};
String[] wehreargs = new String[]{number};
this.openDataBase();
Cursor c = myDataBase.query("mytable", columns, "_id=?", wehreargs, null, null, null)
boolean hasEntry = c.moveToFirst()
c.close();
this.close()
return hasEntry;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.