`
gnibrE
  • 浏览: 136539 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

联系人 数据库 contacts , data , raw_contacts表相关

阅读更多
联系人data表


android.provider.ContactsContract.Data

Constants for the data table, which contains data points tied to a raw contact. For example, a phone number or email address.


Data kinds

Data is a generic table that can hold all kinds of data. Sync adapters and applications can introduce their own data kinds. The kind of data stored in a particular row is determined by the mime type in the row. Fields from DATA1 through DATA15 are generic columns whose specific use is determined by the kind of data stored in the row. For example, if the data kind is Phone.CONTENT_ITEM_TYPE, then DATA1 stores the phone number, but if the data kind is Email.CONTENT_ITEM_TYPE, then DATA1 stores the email address.

ContactsContract defines a small number of common data kinds, e.g. ContactsContract.CommonDataKinds.Phone, ContactsContract.CommonDataKinds.Email etc. As a convenience, these classes define data kind specific aliases for DATA1 etc. For example, Phone.NUMBER is the same as Data.DATA1.

DATA1 is an indexed column and should be used for the data element that is expected to be most frequently used in query selections. For example, in the case of a row representing email addresses DATA1 should probably be used for the email address itself, while DATA2 etc can be used for auxiliary information like type of email address.

By convention, DATA15 is used for storing BLOBs (binary data).

Typically you should refrain from introducing new kinds of data for 3rd party account types. For example, if you add a data row for "favorite song" to a raw contact owned by a Google account, it will not get synced to the server, because the Google sync adapter does not know how to handle this data kind. Thus new data kinds are typically introduced along with new account types, i.e. new sync adapters.

查询操作
// 1 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);

// 2 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);
 





data表存储了大部分的联系人实际信息,他的每一个列的名字之所以这么难看(叫做data1~data15),是因为data表想要存储各种各样的联系人信息,同时不想让每个联系人的数据库列数太长。比如联系人信息里面,光名字就可以分为family name,mid name,nick name;光电话号码可以分为手机,家里,办公室,传真云云;光邮件可以分成这般这般这般那般,相当的纷繁复杂。如果每个信息都作为一个列出现在数据库,那就是硕大一个稀疏矩阵了。
所以data表这样的设计,更实用,也易于扩展;缺点是难看。
不过虽然难看,却不难查询,因为数据库查询的时候,根据data表的mimetype_id来检索需要的内容。
2.1里面,data表的mimetype支持这么十来种,这些不一定能在dump出来的数据库看到,因为一般用不到。。
    *  StructuredName.CONTENT_ITEM_TYPE   //    "vnd.android.cursor.item/name" 

    * Phone.CONTENT_ITEM_TYPE    // "vnd.android.cursor.item/phone_v2"

    * Email.CONTENT_ITEM_TYPE   // "vnd.android.cursor.item/email_v2"
    * Photo.CONTENT_ITEM_TYPE  //    "vnd.android.cursor.item/photo" 
    * Organization.CONTENT_ITEM_TYPE   // .....
    * Im.CONTENT_ITEM_TYPE
    * Nickname.CONTENT_ITEM_TYPE    //  "vnd.android.cursor.item/nickname" 
    * Note.CONTENT_ITEM_TYPE
    * StructuredPostal.CONTENT_ITEM_TYPE
    * GroupMembership.CONTENT_ITEM_TYPE
    * Website.CONTENT_ITEM_TYPE
    * Event.CONTENT_ITEM_TYPE
    * Relation.CONTENT_ITEM_TYPE


对于这些不同类型的查询,有些可以用
ContactsContract.CommonDataKinds.*来完成,
比如Email
Uri uri = Uri.withAppendedPath(Email.CONTENT_LOOKUP_URI, Uri.encode("bob"));
 Cursor c = getContentResolver().query(uri,
          new String[]{Email.DISPLAY_NAME, Email.DATA},
          null, null, null);






RawContacts表

类: android.provider.ContactsContract.RawContacts

Class Overview

Constants for the raw contacts table, which contains the base contact information per sync source. Sync adapters and contact management apps are the primary consumers of this API.

查询
// 1, Finding all raw contacts in a Contact is easy: 
Cursor c = getContentResolver().query(RawContacts.CONTENT_URI,
          new String[]{RawContacts._ID},
          RawContacts.CONTACT_ID + "=?",
          new String[]{String.valueOf(contactId)}, null);

//There are two ways to find raw contacts within a specific account, 
// you can either put the account name and type in the selection or pass them as query parameters. 
// The latter approach is preferable, especially when you can reuse the URI: 

Uri rawContactUri = RawContacts.URI.buildUpon()
          .appendQueryParameter(RawContacts.ACCOUNT_NAME, accountName)
          .appendQueryParameter(RawContacts.ACCOUNT_TYPE, accountType)
          .build();
 Cursor c1 = getContentResolver().query(rawContactUri,
          RawContacts.STARRED + "<>0", null, null, null);
 ...
 Cursor c2 = getContentResolver().query(rawContactUri,
          RawContacts.DELETED + "<>0", null, null, null);

// The best way to read a raw contact along with all the data associated with it is by using the ContactsContract.RawContacts.Entity directory. 
// If the raw contact has data rows, the Entity cursor will contain a row for each data row. 
// If the raw contact has no data rows, the cursor will still contain one row with the raw contact-level information. 

 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId);
 Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY);
 Cursor c = getContentResolver().query(entityUri,
          new String[]{RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1},
          null, null, null);
 try {
     while (c.moveToNext()) {
         String sourceId = c.getString(0);
         if (!c.isNull(1)) {
             String mimeType = c.getString(2);
             String data = c.getString(3);
             ...
         }
     }
 } finally {
     c.close();
 }



关于ContactsContract.RawContactsEntity ,
说明:  Constants for the raw contacts entities table, which can be though of as an outer join of the raw_contacts table with the data table. It is a strictly read-only table.

If a raw contact has data rows, the RawContactsEntity cursor will contain a one row for each data row. If the raw contact has no data rows, the cursor will still contain one row with the raw contact-level information and nulls for data columns.


Contacts表。
直接抄一些过来。。
long  _ID  read-only  Row ID. Consider using LOOKUP_KEY instead.
String LOOKUP_KEY read-only An opaque value that contains hints on how to find the contact if its row id changed as a result of a sync or aggregation.
String DISPLAY_NAME read-only The display name for the contact. During aggregation display name is computed from display names of constituent raw contacts using a heuristic: a longer name or a name with more diacritic marks or more upper case characters is chosen.
long PHOTO_ID read-only Reference to the row in the ContactsContract.Data table holding the photo. That row has the mime type CONTENT_ITEM_TYPE. The value of this field is computed automatically based on the IS_SUPER_PRIMARY field of the data rows of that mime type.
int IN_VISIBLE_GROUP read-only An indicator of whether this contact is supposed to be visible in the UI. "1" if the contact has at least one raw contact that belongs to a visible group; "0" otherwise.
int HAS_PHONE_NUMBER read-only An indicator of whether this contact has at least one phone number. "1" if there is at least one phone number, "0" otherwise.
int TIMES_CONTACTED read/write The number of times the contact has been contacted. See markAsContacted(ContentResolver, long). When raw contacts are aggregated, this field is computed automatically as the maximum number of times contacted among all constituent raw contacts. Setting this field automatically changes the corresponding field on all constituent raw contacts.
long LAST_TIME_CONTACTED read/write The timestamp of the last time the contact was contacted. See markAsContacted(ContentResolver, long). Setting this field also automatically increments TIMES_CONTACTED. When raw contacts are aggregated, this field is computed automatically as the latest time contacted of all constituent raw contacts. Setting this field automatically changes the corresponding field on all constituent raw contacts.
int STARRED read/write An indicator for favorite contacts: '1' if favorite, '0' otherwise. When raw contacts are aggregated, this field is automatically computed: if any constituent raw contacts are starred, then this field is set to '1'. Setting this field automatically changes the corresponding field on all constituent raw contacts.
String CUSTOM_RINGTONE read/write A custom ringtone associated with a contact. Typically this is the URI returned by an activity launched with the ACTION_RINGTONE_PICKER intent.
int SEND_TO_VOICEMAIL read/write An indicator of whether calls from this contact should be forwarded directly to voice mail ('1') or not ('0'). When raw contacts are aggregated, this field is automatically computed: if all constituent raw contacts have SEND_TO_VOICEMAIL=1, then this field is set to '1'. Setting this field automatically changes the corresponding field on all constituent raw contacts.
int CONTACT_PRESENCE read-only Contact IM presence status. See ContactsContract.StatusUpdates for individual status definitions. Automatically computed as the highest presence of all constituent raw contacts. The provider may choose not to store this value in persistent storage. The expectation is that presence status will be updated on a regular basic.
String CONTACT_STATUS read-only Contact's latest status update. Automatically computed as the latest of all constituent raw contacts' status updates.
long CONTACT_STATUS_TIMESTAMP read-only The absolute time in milliseconds when the latest status was inserted/updated.
String CONTACT_STATUS_RES_PACKAGE read-only The package containing resources for this status: label and icon.
long CONTACT_STATUS_LABEL read-only The resource ID of the label describing the source of contact status, e.g. "Google Talk". This resource is scoped by the CONTACT_STATUS_RES_PACKAGE.
long CONTACT_STATUS_ICON read-only The resource ID of the icon for the source of contact status. This resource is scoped by the CONTACT_STATUS_RES_PACKAGE.



------------------------------------
关于查询。

数据库本身就复杂, 查询更是复杂, android提供的api试图把所有的查询变得简单,
不过就像我的程序一样,写了也就自己用,别人都不会用,甚至不愿意看。。。。。
这些api里面有不少整合操作,试图简化查询,因此很多uri的查询结果是很丰富很整合的。


使用URI:Contacts.CONTENT_URI
D/GN>  columname of query uri( 4993):   :  Contacts.CONTENT_URI,
D/GN>     ( 4993):   :    <0>: times_contacted
D/GN>     ( 4993):   :    <1>: contact_status
D/GN>     ( 4993):   :    <2>: custom_ringtone
D/GN>     ( 4993):   :    <3>: has_phone_number
D/GN>     ( 4993):   :    <4>: contact_status_label
D/GN>     ( 4993):   :    <5>: lookup
D/GN>     ( 4993):   :    <6>: contact_status_icon
D/GN>     ( 4993):   :    <7>: last_time_contacted
D/GN>     ( 4993):   :    <8>: display_name
D/GN>     ( 4993):   :    <9>: in_visible_group
D/GN>     ( 4993):   :    <10>: _id
D/GN>     ( 4993):   :    <11>: starred
D/GN>     ( 4993):   :    <12>: contact_presence
D/GN>     ( 4993):   :    <13>: contact_status_res_package
D/GN>     ( 4993):   :    <14>: contact_status_ts
D/GN>     ( 4993):   :    <15>: photo_id
D/GN>     ( 4993):   :    <16>: send_to_voicemail



2  URI:  ContactsContract.CommonDataKinds.Phone.CONTENT_URI

这个查询相当强大
D/GN>  column name of query uri( 4993):   :  ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
D/GN>     ( 4993):   :    <0>: data_version
D/GN>     ( 4993):   :    <1>: contact_id
D/GN>     ( 4993):   :    <2>: lookup
D/GN>     ( 4993):   :    <3>: data12
D/GN>     ( 4993):   :    <4>: data11
D/GN>     ( 4993):   :    <5>: data10
D/GN>     ( 4993):   :    <6>: mimetype
D/GN>     ( 4993):   :    <7>: data15
D/GN>     ( 4993):   :    <8>: data14
D/GN>     ( 4993):   :    <9>: data13
D/GN>     ( 4993):   :    <10>: data_sync1
D/GN>     ( 4993):   :    <11>: data_sync3
D/GN>     ( 4993):   :    <12>: data_sync2
D/GN>     ( 4993):   :    <13>: data_sync4
D/GN>     ( 4993):   :    <14>: account_type
D/GN>     ( 4993):   :    <15>: custom_ringtone
D/GN>     ( 4993):   :    <16>: status
D/GN>     ( 4993):   :    <17>: data1
D/GN>     ( 4993):   :    <18>: data4
D/GN>     ( 4993):   :    <19>: data5
D/GN>     ( 4993):   :    <20>: data2
D/GN>     ( 4993):   :    <21>: data3
D/GN>     ( 4993):   :    <22>: data8
D/GN>     ( 4993):   :    <23>: data9
D/GN>     ( 4993):   :    <24>: group_sourceid
D/GN>     ( 4993):   :    <25>: data6
D/GN>     ( 4993):   :    <26>: account_name
D/GN>     ( 4993):   :    <27>: data7
D/GN>     ( 4993):   :    <28>: display_name
D/GN>     ( 4993):   :    <29>: in_visible_group
D/GN>     ( 4993):   :    <30>: contact_status_res_package
D/GN>     ( 4993):   :    <31>: is_primary
D/GN>     ( 4993):   :    <32>: contact_status_ts
D/GN>     ( 4993):   :    <33>: raw_contact_id
D/GN>     ( 4993):   :    <34>: times_contacted
D/GN>     ( 4993):   :    <35>: contact_status
D/GN>     ( 4993):   :    <36>: status_res_package
D/GN>     ( 4993):   :    <37>: status_icon
D/GN>     ( 4993):   :    <38>: contact_status_icon
D/GN>     ( 4993):   :    <39>: mode
D/GN>     ( 4993):   :    <40>: version
D/GN>     ( 4993):   :    <41>: last_time_contacted
D/GN>     ( 4993):   :    <42>: res_package
D/GN>     ( 4993):   :    <43>: _id
D/GN>     ( 4993):   :    <44>: status_ts
D/GN>     ( 4993):   :    <45>: dirty
D/GN>     ( 4993):   :    <46>: is_super_primary
D/GN>     ( 4993):   :    <47>: photo_id
D/GN>     ( 4993):   :    <48>: send_to_voicemail
D/GN>     ( 4993):   :    <49>: contact_status_label
D/GN>     ( 4993):   :    <50>: status_label
D/GN>     ( 4993):   :    <51>: starred
D/GN>     ( 4993):   :    <52>: contact_presence
D/GN>     ( 4993):   :    <53>: sourceid




3。URI:ContactsContract.Data.CONTENT_URI
传说中的data表。
D/GN>  columname of query uri( 4993):   :  ContactsContract.Data.CONTENT_URI,
D/GN>     ( 4993):   :    <0>: data_version
D/GN>     ( 4993):   :    <1>: contact_id
D/GN>     ( 4993):   :    <2>: lookup
D/GN>     ( 4993):   :    <3>: data12
D/GN>     ( 4993):   :    <4>: data11
D/GN>     ( 4993):   :    <5>: data10
D/GN>     ( 4993):   :    <6>: mimetype
D/GN>     ( 4993):   :    <7>: data15
D/GN>     ( 4993):   :    <8>: data14
D/GN>     ( 4993):   :    <9>: data13
D/GN>     ( 4993):   :    <10>: data_sync1
D/GN>     ( 4993):   :    <11>: data_sync3
D/GN>     ( 4993):   :    <12>: data_sync2
D/GN>     ( 4993):   :    <13>: data_sync4
D/GN>     ( 4993):   :    <14>: account_type
D/GN>     ( 4993):   :    <15>: custom_ringtone
D/GN>     ( 4993):   :    <16>: status
D/GN>     ( 4993):   :    <17>: data1
D/GN>     ( 4993):   :    <18>: data4
D/GN>     ( 4993):   :    <19>: data5
D/GN>     ( 4993):   :    <20>: data2
D/GN>     ( 4993):   :    <21>: data3
D/GN>     ( 4993):   :    <22>: data8
D/GN>     ( 4993):   :    <23>: data9
D/GN>     ( 4993):   :    <24>: group_sourceid
D/GN>     ( 4993):   :    <25>: data6
D/GN>     ( 4993):   :    <26>: account_name
D/GN>     ( 4993):   :    <27>: data7
D/GN>     ( 4993):   :    <28>: display_name
D/GN>     ( 4993):   :    <29>: in_visible_group
D/GN>     ( 4993):   :    <30>: contact_status_res_package
D/GN>     ( 4993):   :    <31>: is_primary
D/GN>     ( 4993):   :    <32>: contact_status_ts
D/GN>     ( 4993):   :    <33>: raw_contact_id
D/GN>     ( 4993):   :    <34>: times_contacted
D/GN>     ( 4993):   :    <35>: contact_status
D/GN>     ( 4993):   :    <36>: status_res_package
D/GN>     ( 4993):   :    <37>: status_icon
D/GN>     ( 4993):   :    <38>: contact_status_icon
D/GN>     ( 4993):   :    <39>: mode
D/GN>     ( 4993):   :    <40>: version
D/GN>     ( 4993):   :    <41>: last_time_contacted
D/GN>     ( 4993):   :    <42>: res_package
D/GN>     ( 4993):   :    <43>: _id
D/GN>     ( 4993):   :    <44>: status_ts
D/GN>     ( 4993):   :    <45>: dirty
D/GN>     ( 4993):   :    <46>: is_super_primary
D/GN>     ( 4993):   :    <47>: photo_id
D/GN>     ( 4993):   :    <48>: send_to_voicemail
D/GN>     ( 4993):   :    <49>: contact_status_label
D/GN>     ( 4993):   :    <50>: status_label
D/GN>     ( 4993):   :    <51>: starred
D/GN>     ( 4993):   :    <52>: contact_presence
D/GN>     ( 4993):   :    <53>: sourceid



列出来发现,跟刚才的phone查询一样的萨?
no。检索出来的内容数量不同。
刚才那个phone的,是这个data的子集,也就是从查询结果中,过滤去了mimetype是phone的部分。
哦。


4 URI: ContactsContract.RawContacts.CONTENT_URI
D/GN>  columname of query uri( 4993):   :  ContactsContract.RawContacts.CONTENT_URI,
D/GN>     ( 4993):   :    <0>: times_contacted
D/GN>     ( 4993):   :    <1>: custom_ringtone
D/GN>     ( 4993):   :    <2>: contact_id
D/GN>     ( 4993):   :    <3>: sync4
D/GN>     ( 4993):   :    <4>: sync3
D/GN>     ( 4993):   :    <5>: sync2
D/GN>     ( 4993):   :    <6>: sync1
D/GN>     ( 4993):   :    <7>: deleted
D/GN>     ( 4993):   :    <8>: version
D/GN>     ( 4993):   :    <9>: account_name
D/GN>     ( 4993):   :    <10>: last_time_contacted
D/GN>     ( 4993):   :    <11>: aggregation_mode
D/GN>     ( 4993):   :    <12>: _id
D/GN>     ( 4993):   :    <13>: starred
D/GN>     ( 4993):   :    <14>: dirty
D/GN>     ( 4993):   :    <15>: sourceid
D/GN>     ( 4993):   :    <16>: send_to_voicemail
D/GN>     ( 4993):   :    <17>: account_type




总而言之一句话,
源码全在 android.provider.ContactsContract , 看完了就不用看我这些唠叨了。。
我是看不完…………看着晕。
分享到:
评论
1 楼 zhaopian16 2011-04-14  
  S0S
我现在想让RawContacts 表查询完个cursor按姓名排序,怎么做???使用ContactsContract.Data.DISPLAY_NAME 他说找不到列名?我晕,数据库里明明有啊!!!!

相关推荐

Global site tag (gtag.js) - Google Analytics