Knowledge about contacts in content provider

Source: Internet
Author: User
Tags sqlite manager

The contactscontract class has many embedded classes to help operate the contracts in the contect provider. Among them, many data tables corresponding to the database are used (more important) the raw_contacts table corresponding to the rawcontrats class and the groups table corresponding to the groups class. Data Tables are very important for storing various types of data. They are differentiated by the mimetype class and stored in data1 ~ In data15, different types, you need to view the corresponding mimetype, for example, there are some types (from the http://developer.android.com/reference/android/provider/ContactsContract.Data.html ):

The MIME type of the item represented by this row. Examples of common MIME types are:

  • StructuredName.CONTENT_ITEM_TYPE
  • Phone.CONTENT_ITEM_TYPE
  • Email.CONTENT_ITEM_TYPE
  • Photo.CONTENT_ITEM_TYPE
  • Organization.CONTENT_ITEM_TYPE
  • Im.CONTENT_ITEM_TYPE
  • Nickname.CONTENT_ITEM_TYPE
  • Note.CONTENT_ITEM_TYPE
  • StructuredPostal.CONTENT_ITEM_TYPE
  • GroupMembership.CONTENT_ITEM_TYPE
  • Website.CONTENT_ITEM_TYPE
  • Event.CONTENT_ITEM_TYPE
  • Relation.CONTENT_ITEM_TYPE
  • SipAddress.CONTENT_ITEM_TYPE

The following table shows how to use SQLite manager to view the keys in the data table of provider. Contacts exported from the virtual machine, which corresponds to the development document of Android developer.

Data Table

Similarly, rawcontacts and groups tables also correspond to each other. It is worth noting that these tables are related to each other. For example, the raw_contact_id in the data table is associated with _ ID in the raw_contacts table, the _ ID in the group table corresponds to the data1 in the data table (provided that mimetype = contactscontract. commondatakinds. groupmemebership). Therefore, the data table can be further associated with other tables, so the data table should be the intermediary of all tables. Of course, there is also redundancy between these tables. The purpose of speculation is to facilitate the search, so if you want to change the table, make sure that there are any associated tables that need to be modified accordingly (as if there is an implicit synchronization operation method here, as long as one table is operated, the synchronization mechanism will change the associated table accordingly, to be studied later)


Several operation codes are also recommended for reference:

Insert

An individual data row can be inserted using the traditionalinsert(Uri, ContentValues)Method.
Multiple rows shoshould always be inserted as a batch.

An example of a traditional insert:

 ContentValues values = new ContentValues(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); values.put(Phone.NUMBER, "1-800-GOOG-411"); values.put(Phone.TYPE, Phone.TYPE_CUSTOM); values.put(Phone.LABEL, "free directory assistance"); Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values); 

The same done using contentprovideroperations:

 ArrayList<ContentProviderOperation> ops =          new ArrayList<ContentProviderOperation>(); ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)          .withValue(Data.RAW_CONTACT_ID, rawContactId)          .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)          .withValue(Phone.NUMBER, "1-800-GOOG-411")          .withValue(Phone.TYPE, Phone.TYPE_CUSTOM)          .withValue(Phone.LABEL, "free directory assistance")          .build()); getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 
Update

Just as with insert, update can be done incrementally or as a batch, the batch mode being the preferred method:

 ArrayList<ContentProviderOperation> ops =          new ArrayList<ContentProviderOperation>(); ops.add(ContentProviderOperation.newUpdate(Data.CONTENT_URI)          .withSelection(Data._ID + "=?", new String[]{String.valueOf(dataId)})          .withValue(Email.DATA, "somebody@android.com")          .build()); getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 
Delete

Just as with insert and update, deletion can be done either usingdelete(Uri, String, String[])
Method or using a contentprovideroperation:

 ArrayList<ContentProviderOperation> ops =          new ArrayList<ContentProviderOperation>(); ops.add(ContentProviderOperation.newDelete(Data.CONTENT_URI)          .withSelection(Data._ID + "=?", new String[]{String.valueOf(dataId)})          .build()); getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops); 
Query
Finding all data of a given type for a given contact
 Cursor c = getContentResolver().query(Data.CONTENT_URI,          new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},          Data.CONTACT_ID + "=?" + " AND "                  + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",          new String[] {String.valueOf(contactId)}, null); 

Finding all data of a given type for a given raw contact
 Cursor c = getContentResolver().query(Data.CONTENT_URI,          new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},          Data.RAW_CONTACT_ID + "=?" + " AND "                  + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",          new String[] {String.valueOf(rawContactId)}, null);

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.