I am trying to query data from sqlite db through CursorLoader. When i go through the documentation in android developer website, i can\'t find the limit query.
Cu
I realize this is late, but sandrstar gave me an idea which was to use the Uri that you pass into the ContentProvider and append the limit as the last path segment to it using
Uri.withAppendedPath("uri" "/" + limitNumber);
Then inside your content provider, in your URI matcher make sure you match
MATCHER.addURI("authority", "uri" + "/#", matchInt);
Then in your query, check for the matchInt that you matched the URI to and get the last path segment
uri.getLastPathSegment();
Which will be your limit and then use what sandrstar recommended with the SQLiteQueryBuilder.
Just figured it might be nice to have a good technique to get your parameter to the Provider. Hope this helps!
You can use SQLiteQueryBuilder methods whose provides ability to have LIMIT
clause.
Or You can just append LIMIT
to Yours selection string.
Also, there's an option to provide limit via uri (as part of it) and handle it in your ContentProvider.
Looks like CursorLoader
is not providing any API for it currently.
The hackish way:
String sortOrder = "ROWID LIMIT 5"
which will result in ORDER BY ROWID LIMIT 5
. ROWID would sort by the implicit row ids that SQLite keeps - very close to what happens when you don't specify a sort oder at all.
Not that this is abusing the fact that the order parameter does not check if the supplied value is just a valid order type. Kind of a buffer overflow attack.
A better way would be to define Uri
parameters.
// query code
Uri queryUri = Uri.parse("content://what/ever/items");
queryUri = queryUri.buildUpon().appendQueryParameter("limit", "5").build();
// in ContentProvider
String limit = queryUri.getQueryParameter("limit");
Both approaches will only work if the ContentProvider
is compatible with above attempts to limit. There is actually no well-defined way to limit or select certain data from a ContentProvider
. Each ContentProvider
comes with it's own contract so above will not work with every provider.