Android/SQLite-对WHERE子句的位操作 [英] Android/SQLite - Bit operation on WHERE clause

查看:39
本文介绍了Android/SQLite-对WHERE子句的位操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在Android中执行以下操作:

I'd like to know if it's possible to do in Android something like this:

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.它应该可以工作,但不能这样做:我在做什么错了?

FLAGS is a 1-byte bit mask and I'd like to select only the rows which has the second bit (position 1) of the mask on. The mask (Flow.FLOW_TYPE) is 0b00000010 and the type parameter can be either 0 or 1. It should work but it doesn't: what am I doing wrong?

推荐答案

query 函数仅接受字符串作为参数,但是在SQLite中,数字和字符串绝不能相等(除非您具有类型相似性,但这仅适用于列值,不适用于表达式).

The query function accepts only strings as parameters, but in SQLite, numbers and strings never compare equal (unless you have type affinity, but this works only for column values, not expressions).

您必须将参数显式转换回数字:

You have to explicitly convert the parameters back to a number:

((...) >> 1 = CAST(? AS INTEGER)) AND ...

这篇关于Android/SQLite-对WHERE子句的位操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆