Skip to content Skip to sidebar Skip to footer

How Add Limit Clause To Managequery On Android

Android's API provides a clean mechanism via SQLite to make queries into the contact list. However, I am not sure how to limit the results: Cursor cur = ((Activity)mCtx).managedQu

Solution 1:

Actually, depending on the provider you can append a limit to the URI as follows:

uri.buildUpon().appendQueryParameter("limit", "40").build()

I know the MediaProvider handles this and from looking at recent code it seems you can do it with contacts too.

Solution 2:

You are accessing a ContentProvider, not SQLite, when you query the Contacts ContentProvider. The ContentProvider interface does not support a LIMIT clause directly.

If you are directly accessing a SQLite database of your own, use the rawQuery() method on SQLiteDatabase and add a LIMIT clause.

Solution 3:

I found out from this bug that Android uses the following regex to parse the LIMIT clause of a query:

From <framework/base/core/java/android/database/sqlite/SQLiteQueryBuilder.java>

LIMIT clause is checked with following sLimitPattern.

privatestaticfinalPattern sLimitPattern =Pattern.compile("\\s*\\d+\\s*(,\\s*\\d+\\s*)?");

Note that the regex does accept the format offsetNumber,limitNumber even though it doesn't accept the OFFSET statement directly.

Solution 4:

I think you have to do this sort of manually. The Cursor object that is returned from the managedQuery call doesn't execute a full query right off. You have to use the Cursor.move*() methods to jump around the result set.

If you want to limit it, then create your own limit while looping through the results. If you need paging, then you can use the Cursor.moveToPosition(startIndex) and start reading from there.

Solution 5:

You can specify the "limit" parameter in the "order" parameter, maybe even inside other parameters if you don't want to sort, because you'll have to specify a column to sort by then:

mContentResolver.query(uri, columnNames, null, null, "id LIMIT 1");

Post a Comment for "How Add Limit Clause To Managequery On Android"