简体   繁体   中英

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?

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;
    }
}

query returns a Cursor which is always non-null. You must read from the Cursor to get the result of the query

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM