sqlite 选择 returns 没有结果

sqlite selection returns no results

我有这个table:

我尝试 select 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;
}

但我得到的结果集是空的。

我需要修复什么?

替换:

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

作者:

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

您要查询 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);