Android Contacts Database Structure

Android contacts is a built-in application. It also provide content providers which the third party app can use to manipulate android contacts, for example add, delete, update and query contacts. This article will introduce detail information about how the android contacts is implemented.

1. Android Contacts Overview.

In an android device, you can click Phone —> Contacts icon to open the contacts list. When you click each contact you can see a lot of contact info listed such as mobile phone, work phone, display name, nick name, company, department, title, Website etc.

android phone icon
android contacts list
android contact detail info

All the infomation for one contact is divided into 13 groups.

  1. Email ( address, email type(home, work, mobile, other)).
  2. Instant message ( protocol(qq, icq, skype etc), im id).
  3. Nickname.
  4. Organization ( company, department, title, job description,  office location).
  5. Phone ( number, phone type(home, mobile, work, other)).
  6. Address ( address, address type ( home, work , other)).
  7. Name (display name, given name, family name).
  8. Postal Address (country, city, region, street, postcode).
  9. Identity (namespace(SSN, passport), number).
  10. Photo.
  11. Group (contact belongs group id).
  12. Website (website url, website type()).
  13. Note.

For some groups, such as email, phone, address etc. The info is also classified according to data usage, such as use at home, at work and others etc.

2. Where Are Contacts Stored In Android File System.

All the android contact information is saved in SQLite database. The database file is located at /data /data /com.android.providers.contacts /databases /contacts2.db

You can use android device monitor to browse above database file as below. If you can not open above folder, run adb root shell command in a dos or shell window.

use android device monitor to browse android contacts database file

2.1 Operate Contacts Table Online Use SQLite3.

You can run below shell commands to operate the contacts table and data online.

C:\Users\Jerry>adb shell 
generic_x86:/ $ su 
generic_x86:/ # sqlite3 /data/data/com.android.providers.contacts/databases/contacts2.db 
SQLite version 3.9.2 2015-11-02 18:31:45 
Enter ".help" for usage hints. 
sqlite> select * from data; 
sqlite> delete from data; 
sqlite> delete from contacts; 
sqlite> delete from raw_contacts;

You can also click the top right button in android device monitor to download the contacts database file to your local PC. And then use a client tool such as SQLiteSpy to browse the contacts database tables.

READ :   Google Map Example In Android Studio

Please read article Android Device Monitor Cannot Open Data Folder Resolve Method to learn how to use android device monitor.

sqlitespy opened android contacts table list

Above picture just use SQLiteSpy to list all the tables in the android contacts database file contacts2.db.

From above picture, we can see that there are five key tables that are most important to know to store contacts information.

  1. groups : Store contact group information.
  2. contacts : Store contacts information.
  3. raw_contacts : Has same _id value with contacts table, the difference is this table contains more contacts information columns than contacts table.
  4. mimetypes : Defines each contact information mime type.
  5. data : Store each contact field in one row, such as mobile phone, home phone, home email, work email, company, department, title etc. Each row has a mimetype_id column, it’s value indicate what that row data means.

3. Key Android Contacts Tables.

3.1 groups table.

Before add any contact in android, you had better create a new group or select an exist contact group from this table.

Double click the table name in SQLiteSpy left panel, it will list all the table columns in right panel.

The following two columns are most important.

  1. title : This is the group title you provided when you create it.
  2. notes : This column save the group description info.

android contact table groups

3.2 contacts table.

android contact table contacts

This table store contact id related information. It do not store contact summary information. Below columns is important in this table.

  1. _id : contact id.
  2. last_time_contacted : The last time this contact is contacted. The format is in milliseconds.
  3. contact_last_updated_timestamp : The timestamp this contact is updated for the last time.

3.3 raw_contacts table.

android contact table raw_contacts

This table is similar with contacts table. But it contains more detail summary contact information than contacts table. Such as display_name, sort_key etc.

READ :   Android Activity Example - Switch Between Screens

raw_contacts table columns.

  1. _id : The value is same in contacts table.
  2. display_name : The display name for this contact. Include first name, family name etc.

3.4 mimetypes table.

android contact table mimetypes

mimetypes table is the most important table for android contacts. It has only 2 columns, and has only 13 rows data.

mimetypes table columns.

  1. _id : mimetype id.
  2. mimetype : Used to define android contact filed mime type.
    For example for contact information email field, it’s mimetype is vnd.android.cursor.item/email_v2.

mimetype values.

All those mimetype _id will be referenced in data table, it indicate what contact info that row of data stores.

  1. vnd.android.cursor.item/email_v2 : Indicate this row store contact email info.
  2. vnd.android.cursor.item/im : The row store instant messaging info.
  3. vnd.android.cursor.item/nickname : The row store nickname info.
  4. vnd.android.cursor.item/organization : The row store contact organization info.
  5. vnd.android.cursor.item/phone_v2 : The row store contact phone info.
  6. vnd.android.cursor.item/sip_address :  The row store contact address info.
  7. vnd.android.cursor.item/name : The row store contact name info.
  8. vnd.android.cursor.item/postal-address_v2 : The row store postal address info.
  9. vnd.android.cursor.item/identity : The row store contact identity info.
  10. vnd.android.cursor.item/photo : The row store contact photo info.
  11. vnd.android.cursor.item/group_membership : The row store contact group info.
  12. vnd.android.cursor.item/website : The row store contact website info.
  13. vnd.android.cursor.item/note : The row store contact note info.

3.5 data table.

android contact table data

Data table is the core table that store android contact information. Each row store one contact information field.

The contact filed data type is identified by mimetype_id column value, and the contact user is identified by raw_contact_id column.

Other columns from data1 to data15 is used to store contact info filed values.

3.5.1 data table columns.
  1. mimetype_id : Reference _id column in mimetypes table. Used to indicate what data is stored in this row.
  2. raw_contact_id : Reference _id column in raw_contacts table. Used to indicate which contact this row belongs to.
  3. data1 to data15 : For different mimetype, the data column stores different data value. You can read article Android Contacts Fields, Data Table Columns And Data Mimetype Explain to learn more.
(Visited 3,461 times, 20 visits today)

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.