Could someone advise me why I am only getting true
result, even though that record does not exist in my database?
For clarification, in my table, _id
is the telephone number.
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:
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.