Android Sqlite3 database: Cursor in SQLite is not returning null? -
could advise me why getting true result, though record not exist in database?
for clarification, in table, _id telephone number.
here's 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 returning non-null cursors only: revised 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; }
Comments
Post a Comment