2

I'm trying to do a relatively simple bitwise query operation with SQLite on Android. When I use bind variables, I get no data returned when I believe should get some rows back. If I hardcode the bind variable's value directly into the SQL, it works just fine. I'm thinking I have some silly syntax issue somewhere, but I just can't see it.

So this code works just fine:

String selection = new String(FLAGS + " & 2 = 2");
cursor = db.query(TABLE_NAME, ALL_COLUMNS, selection,
    null, null, null, null, null );

This code however (using bind variables), returns no rows:

String selection = new String(FLAGS + " & ? = ?");
String[] selectionArgs = new String[]{"2", "2"};
cursor = db.query(TABLE_NAME, ALL_COLUMNS, selection,
    selectionArgs, null, null, null, null );

They both result into a syntactically identical query being built when I inspect the cursor's mQuery property through the debugger. The latter does have the mBindArgs property populated correctly as well. I'm at a loss as to how this could be failing. There are no exceptions thrown or anything, it just doesn't return any rows.

I can take the failing query, and manually swap the question marks for the two's and paste it into the ADB SQLite command line interface and it works just fine as well.

2 Answers 2

1

If I am right the second query produces following condition:

& '2' = '2'

instead of

& 2 = 2

Try replacing & with AND

Make sure you have no ? characters in FLAG constant.

Besides what is a point of this logical condition?

Sign up to request clarification or add additional context in comments.

1 Comment

It's a bitwise operation, I need to use the &. I'm testing which bits of a flag value are set. I thought about the quotes too, but that would not be consistent, as far as I can tell. The mQuery property of the cursor, which shows the built SQL does not show quotes around the question marks either.
1

I was facing the same problem as yours. As radek-k said, the query compares string.

One solution that may be performed is to use the following:

String selection = new String(FLAGS + " & ? = (0|?)");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.