sqlite选择不返回任何结果

时间:2015-01-04 07:58:42

标签: java android database sqlite

我有这张桌子:

enter image description here

我尝试选择phone = 9isBlocked = 1

SQLiteDirectCursorDriver: SELECT KEY_ID FROM BLOCKED_PHONES_TABLE WHERE KEY_PHONE = ? AND KEY_IS_BLOCKED = ?

代码:

    Cursor cursor = this.getReadableDatabase().query(
            BLOCKED_PHONES_TABLE,
            new String[] { KEY_ID  }, KEY_PHONE+" = ? AND "+KEY_IS_BLOCKED+" = ?",
            new String[] { phone, "1" }, null, null, null);

      while(cursor.moveToNext()) {
        result = new Phone();
        result.id = (cursor.getInt(0));
        result.phone = phone;
        result.isBlocked = true;
    }
    return result;
}

但我得到空结果。

我需要修理什么?

enter image description here

2 个答案:

答案 0 :(得分:0)

您要求KEY_IS_BLOCKED列包含字符串1的行。

显然,你没有任何行,这个值就是这样一个字符串。

Android数据库API仅支持字符串值作为参数。 只需将数字直接插入SQL查询:

Cursor cursor = this.getReadableDatabase().query(
        BLOCKED_PHONES_TABLE,
        new String[] { KEY_ID },
        KEY_PHONE + " = ? AND " + KEY_IS_BLOCKED + " = 1" + ,
        new String[] { phone },
        null, null, null);

答案 1 :(得分:-2)

替换:

new String[] { phone, "1" }, null, null, null);

通过

new String[] { String.valueOf(phone), "1" }, null, null, null);

并替换:

while(cursor.moveToNext()) {
    result = new Phone();
    result.id = (cursor.getInt(0));
    result.phone = phone;
    result.isBlocked = true;
}

by:

if(cursor!=null){
    cursor.moveToFirst()
    result = new Phone();
    result.id = (cursor.getInt(0));
    //TODO : print ID_KEY to check result...
}