how add limit clause to manageQuery on android

前端 未结 5 1894
没有蜡笔的小新
没有蜡笔的小新 2021-02-10 03:00

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 = ((Acti         


        
相关标签:
5条回答
  • 2021-02-10 03:18

    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.

    0 讨论(0)
  • 2021-02-10 03:19

    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.

    0 讨论(0)
  • 2021-02-10 03:21

    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.

    0 讨论(0)
  • 2021-02-10 03:26

    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");
    
    0 讨论(0)
  • 2021-02-10 03:39

    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.

    private static final Pattern 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.

    0 讨论(0)
提交回复
热议问题