Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android content resolver query placeholder integer

I am trying to run a simple query on the android Browser Content Provider. The bookmarks are mixed with history items, so to get the bookmarks only one needs to select "Browser.BookmarkColumns.BOOKMARK = 1". Using

String mSelectionClause = Browser.BookmarkColumns.BOOKMARK + " = 1";

with

Cursor cursor = getContentResolver().query(Browser.BOOKMARKS_URI, null, mSelectionClause, null, null);

works fine. But all my attempts to get the same result with a placeholder fail:

String mSelectionClause = Browser.BookmarkColumns.BOOKMARK + " = ?";

The query:

Cursor cursor = getContentResolver().query(Browser.BOOKMARKS_URI, null, mSelectionClause, mSelectinArgs, null);

returns an empty Cursor in all these cases:

mSelectionArgs[0] = "1"
mSelectionArgs[0] = '1'
mSelectionArgs[0] = String.valueOf(1)
mSelectionArgs[0] = Integer.toString(1)
mSelectionArgs[0] = 1 + "" (idea picked up on this site)

Am I doing something wrong or should I give up the hope of using a placeholder?

Cheers.

like image 553
sw-dev Avatar asked Apr 24 '26 08:04

sw-dev


1 Answers

The Android database API was designed for Strings and not Integers. Some people says it was a mistake in the design of the API.

Anyway, in your example String mSelectionClause = Browser.BookmarkColumns.BOOKMARK + " = 1"; you are using an integer (= 1). This is the best and only way to deal with Integers. It is also safe to do so. If you were to use strings, your second solution would be the safest.

like image 69
Quanturium Avatar answered Apr 26 '26 21:04

Quanturium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!