0

有人可以告诉我为什么我只得到true结果,即使我的数据库中不存在该记录?

为了澄清,在我的表中,_id是电话号码。

这是我的代码:

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

2 回答 2

1

query返回一个Cursor总是非空的。您必须从 中读取Cursor以获取结果query

于 2012-06-04T20:38:39.080 回答
0

正如@DougCurrie 提到的:查询只返回非空游标:修改你的代码:

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;
}
于 2012-06-04T20:55:02.840 回答