Use group by in contentresolver

Source: Internet
Author: User

If you want to use contentprovider to query text messages, you can use group by in contentresolver. query to find that the system does not provide interfaces or available fields.

Exploring

First, let's look at the query function:

public final Cursor query(Uri uri, String[] projection,        String selection, String[] selectionArgs, String sortOrder) {    return query(uri, projection, selection, selectionArgs, sortOrder, null);}

The most likely thing to handle is selection. First, we try to setselection = "gourp by thread_id"Execute the program and find from the error log that the SQL statement is compiled with brackets, as shown below:

SELECT _id, thread_id, address, person, body, date FROM sms WHERE (type=1) AND (group by thread_id) ORDER BY date DESC

It seems that to get the correct SQL statement, the selection settings need to be more skillful. Try againselection = "0=0) group by (thread_id"This time! We mainly pay attention to the processing of parentheses, and there must be a query condition behind and. Here we use0=0(Verify here0==0This permanent query is only used to ensure the correctness of SQL statements.

The final processing result is as follows:

Uri SMS_PROVIDER = Uri.parse("content://sms/inbox");String[] projection = new String[] {  "_id", "thread_id", "address",  "person", "body", "date"};Cursor cursor = context.getContentResolver().query(SMS_PROVIDER,  projection, "0=0) group by (thread_id", null, "date desc");

In this way, the compiled SQL statement is:

SELECT _id, thread_id, address, person, body, date FROM sms WHERE (type=1) AND (0=0)group by (thread_id) ORDER BY date DESC

 

Use group by in contentresolver

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.