我有这张桌子:
我尝试选择phone = 9
和isBlocked = 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;
}
但我得到空结果。
我需要修理什么?
答案 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...
}