Zardaloop
Zardaloop

Reputation: 1634

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

Upvotes: 0

Views: 507

Answers (2)

Rafael T
Rafael T

Reputation: 15689

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

Upvotes: 0

Doug Currie
Doug Currie

Reputation: 41220

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

Upvotes: 1

Related Questions