Android/SQLite

编程入门 行业动态 更新时间:2024-10-26 20:29:50
本文介绍了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

本文发布于:2023-11-22 00:48:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1615319.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Android   SQLite

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!