我想知道在Android中是否可以这样做:
public Cursor getFlowsByCategory(int type, int categoryID, int limit) {
SQLiteDatabase db = dbHelper.getReadableDatabase();
final String[] columns = {ID, FLAGS, SUBJECT, AMOUNT, AMOUNT_NO, CATEGORY};
final String selection = "((" + FLAGS + " & ?) >> 1 = ?) AND (" + CATEGORY + " = ?)";
final String[] selectionArgs = {Integer.toString(Flow.FLOW_TYPE), Integer.toString(type), Integer.toString(categoryID)};
return db.query(TABLE, columns, selection, selectionArgs, null, null, ID + " DESC", Integer.toString(limit));
}
FLAGS是一个1字节的位掩码,我想只选择掩码的第二位(位置1)的行。掩码(Flow.FLOW_TYPE)是0b00000010,类型参数可以是0或1.它应该可以工作,但它没有:我做错了什么?
答案 0 :(得分:6)
query
函数只接受字符串作为参数,但在SQLite中,数字和字符串永远不会相等(除非你有type affinity,但这只适用于列值,而不适用于表达式。)
您必须将参数显式转换回数字:
((...) >> 1 = CAST(? AS INTEGER)) AND ...