0

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

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.