Buffalo
Buffalo

Reputation: 4052

Specifying limit / offset for ContentProvider queries

I'm trying to get my ContentResolver to run this query:

select * from myTable limit 1 offset 2

The only query method in ContentResolver is:

resolver.query(uri, projection, selection, selectionArgs, sortOrder);

I've tried:

final Cursor c = resolver.query(
        MyTable.CONTENT_URI,
        MyTable.PROJECTION,
        " ? ?",
        new String[] {"1", "2"},
        null);

Which just throws an IllegaLArgumentException. What is the correct way of achieving this?

Upvotes: 19

Views: 14923

Answers (4)

Avinash R
Avinash R

Reputation: 3151

If you are providing your content provider, then you can use android.net.Uri.Builder#appendQueryParameter for providing limit and offset as query parameters, which the content provider can use while building the query.

public class MyProvider extends ContentProvider {
    public static final String QUERY_PARAMETER_LIMIT = "limit";
    public static final String QUERY_PARAMETER_OFFSET = "offset";

    public Cursor query(Uri uri, ...) {
        String limit = uri.getQueryParameter(QUERY_PARAMETER_LIMIT);
        String offset = uri.getQueryParameter(QUERY_PARAMETER_OFFSET);

        SQLiteQueryBuilder builder = new SQLiteQueryBuilder();

        // set the table name,...

        // leaving handling of null conditions as an exercise to the reader.
        String limitString = offset + "," + limit;

        Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder, limitString);

        //...

        return c;
    }
}

while building the query:

private static final Uri CONTENT_URI = MyProvider.CONTENT_URI.buildUpon()
        .appendQueryParameter(MyProvider.QUERY_PARAMETER_LIMIT,
                    String.valueOf(limit))
        .appendQueryParameter(MyProvider.QUERY_PARAMETER_OFFSET,
                    String.valueOf(offset))
        .build();

note that the android.net.Uri.Builder#appendQueryParameter encodes the value to prevent sql injection.

References:

  1. http://laviefrugale.blogspot.com/2012/01/using-limit-with-android-contentprovider.html
  2. http://www.sqlite.org/lang_select.html
  3. https://stackoverflow.com/a/12476458/1523910 + @eocanha's answer

Upvotes: 25

Ian Warwick
Ian Warwick

Reputation: 4784

I put the LIMIT clause in the sordOrder parameter, I've also seen the same thing done by others but not sure if its 100% correct:

final Cursor c = resolver.query(
        MyTable.CONTENT_URI,
        MyTable.PROJECTION,
        null,
        null,
        " limit 1 offset 2");

Upvotes: 26

Abhinav Saxena
Abhinav Saxena

Reputation: 2044

When I tried to use the limit String [see limit String below] using the following:

 StringBuilder sbLimit = new StringBuilder().append(" ").append(i_offset).append(" , ").append(i_limit);
String limit = sbLimit .toString()

This gave me good results in combination with the select queries , sorting and grouping.

Upvotes: 0

eocanha
eocanha

Reputation: 506

I put the limit clause as a query parameter using the syntax 'limit = offset, limit':

Cursor c = context.getContentResolver().query(
        MyTable.CONTENT_URI.buildUpon().encodedQuery("limit="+offset+","+limit).build(),
        MyTable.PROJECTION,
        null,
        null,
        null);

It works at least with MediaStore uris. Be careful of not encoding the "," or it won't work.

Upvotes: 26

Related Questions