简体   繁体   English

Android Sqlite3数据库:SQLite中的游标未返回null?

[英]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? 有人可以告诉我为什么即使我的数据库中不存在该记录,我还是只能得到true结果吗?

For clarification, in my table, _id is the telephone number. 为了澄清起见,在我的表格中, _id是电话号码。

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. query返回的Cursor始终为非null。 You must read from the Cursor to get the result of the query 您必须从Cursor读取以获取query结果

As @DougCurrie mentions: Query is returning non-Null cursors only: revised your code: 正如@DougCurrie所提到的:查询仅返回非null游标:修改了代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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