Create a Contact and see it in Sqlite

 

In the Android 4.0 Launcher, the app that lists our contacts has an icon of a round-headed person with a blue smile, and is labelled People (not Contacts). The three tabs of the app are for groups, contacts, and favorites. The bottom of the screen should display the lower half of a split action bar with icons for search and add a new contact, but the bar is missing when the app is launched for the first time.

Get the People app to display the icon for “add a new contact”

There are two ways to get the People app to display the bar. The first way is is to launch the Phone app (its icon is a blue telephone), press the Android menu button, select “New contact” from the overflow menu at the bottom, and add the first contact.
Your new contact won’t be backed up. Add an account that backs up contacts online?
Keep local.
Once you have your first contact, the People app will display the bar. You can then delete your first contact if you wish.

The second way is to go to the Launcher, press the Android menu button, select “Manage apps” from the overflow menu at the bottom of the screen, select the app Contacts Storage, and press “Clear data”.
Delete app data? OK
Return to the Launcher, and the People app will display the bar.

Add three contacts in the People app

Add three contacts named Moe, Larry, and Curly. See The Three Stooges.
+
Your new contact won’t be backed up. Add an account that backs up contacts online?
Keep local.
Name: Moe
✓ DONE
Contact saved.

The sqlite3 database

adb shell
# pwd
/

# ls -l     (lowercase LS minus L)

# cd /data/data/com.android.providers.contacts/databases
# pwd
/data/data/com.android.providers.contacts/databases

# ls -l
-rw-rw---- app_9    app_9      136192 2011-12-12 21:00 contacts2.db
-rw-rw---- app_9    app_9           0 2011-12-12 21:00 contacts2.db-journal
-rw-r--r-- root     root            0 2011-12-13 07:24 help
-rw-rw---- app_9    app_9      135168 2011-12-13 07:05 profile.db
-rw-rw---- app_9    app_9           0 2011-12-13 07:05 profile.db-journal

# sqlite3 contacts2.db
sqlite> .help     (Statements with leading dot require no semicolon.)

sqlite> .tables
_sync_state               phone_lookup              view_data_usage_stat
_sync_state_metadata      photo_files               view_entities
accounts                  properties                view_groups
activities                raw_contacts              view_raw_contacts
agg_exceptions            search_index              view_raw_entities
android_metadata          search_index_content      view_stream_items
calls                     search_index_docsize      view_v1_contact_methods
contacts                  search_index_segdir       view_v1_extensions
data                      search_index_segments     view_v1_group_membership
data_usage_stat           search_index_stat         view_v1_groups
default_directory         settings                  view_v1_organizations
directories               status_updates            view_v1_people
groups                    stream_item_photos        view_v1_phones
mimetypes                 stream_items              view_v1_photos
name_lookup               v1_settings               visible_contacts
nickname_lookup           view_contacts             voicemail_status
packages                  view_data

sqlite> .dump raw_contacts
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;

CREATE TABLE raw_contacts (
	_id INTEGER PRIMARY KEY AUTOINCREMENT,
	account_name STRING DEFAULT NULL,
	account_type STRING DEFAULT NULL,
	data_set STRING DEFAULT NULL,
	sourceid TEXT,
	raw_contact_is_read_only INTEGER NOT NULL DEFAULT 0,
	version INTEGER NOT NULL DEFAULT 1,
	dirty INTEGER NOT NULL DEFAULT 0,
	deleted INTEGER NOT NULL DEFAULT 0,
	contact_id INTEGER REFERENCES contacts(_id),
	aggregation_mode INTEGER NOT NULL DEFAULT 0,
	aggregation_needed INTEGER NOT NULL DEFAULT 1,
	custom_ringtone TEXT,
	send_to_voicemail INTEGER NOT NULL DEFAULT 0,
	times_contacted INTEGER NOT NULL DEFAULT 0,
	last_time_contacted INTEGER,
	starred INTEGER NOT NULL DEFAULT 0,
	display_name TEXT,
	display_name_alt TEXT,
	display_name_source INTEGER NOT NULL DEFAULT 0,
	phonetic_name TEXT,
	phonetic_name_style TEXT,
	sort_key TEXT COLLATE PHONEBOOK,
	sort_key_alt TEXT COLLATE PHONEBOOK,
	name_verified INTEGER NOT NULL DEFAULT 0,
	sync1 TEXT,
	sync2 TEXT,
	sync3 TEXT,
	sync4 TEXT
);

CREATE INDEX raw_contacts_contact_id_index ON raw_contacts (contact_id);
CREATE INDEX raw_contacts_source_id_index ON raw_contacts (sourceid, account_type, account_name);
CREATE INDEX raw_contacts_source_id_data_set_index ON raw_contacts (sourceid, account_type, account_name, data_set);
CREATE TRIGGER raw_contacts_deleted    BEFORE DELETE ON raw_contacts BEGIN    DELETE FROM data     WHERE raw_contact_id=OLD._id;   DELETE FROM agg_exceptions     WHERE raw_contact_id1=OLD._id        OR raw_contact_id2=OLD._id;   DELETE FROM visible_contacts     WHERE _id=OLD.contact_id       AND (SELECT COUNT(*) FROM raw_contacts            WHERE contact_id=OLD.contact_id           )=1;   DELETE FROM default_directory     WHERE _id=OLD.contact_id       AND (SELECT COUNT(*) FROM raw_contacts            WHERE contact_id=OLD.contact_id           )=1;   DELETE FROM contacts     WHERE _id=OLD.contact_id       AND (SELECT COUNT(*) FROM raw_contacts            WHERE contact_id=OLD.contact_id           )=1; END;

CREATE TRIGGER raw_contacts_marked_deleted    AFTER UPDATE ON raw_contacts BEGIN    UPDATE raw_contacts     SET version=OLD.version+1      WHERE _id=OLD._id       AND NEW.deleted!= OLD.deleted; END;

CREATE INDEX raw_contact_sort_key1_index ON raw_contacts (sort_key);
CREATE INDEX raw_contact_sort_key2_index ON raw_contacts (sort_key_alt);
COMMIT;

sqlite> select * from raw_contacts;     (SQL requires semicolon.)
1|||||0|2|1|0|1|0|0||0|0||0|Moe|Moe|40||3|Moe|Moe|0||||
2|||||0|2|1|0|2|0|0||0|0||0|Larry|Larry|40||3|Larry|Larry|0||||
3|||||0|2|1|0|3|0|0||0|0||0|Curly|Curly|40||3|Curly|Curly|0||||

sqlite> select * from raw_contacts order by display_name;

sqlite> .headers on
sqlite> .mode line
sqlite> select * from raw_contacts;
                     _id = 1
            account_name =
            account_type =
                data_set =
                sourceid =
raw_contact_is_read_only = 0
                 version = 2
                   dirty = 1
                 deleted = 0
              contact_id = 1
        aggregation_mode = 0
      aggregation_needed = 0
         custom_ringtone =
       send_to_voicemail = 0
         times_contacted = 0
     last_time_contacted =
                 starred = 0
            display_name = Moe
        display_name_alt = Moe
     display_name_source = 40
           phonetic_name =
     phonetic_name_style = 3
                sort_key = Moe
            sort_key_alt = Moe
           name_verified = 0
                   sync1 =
                   sync2 =
                   sync3 =
                   sync4 =

                     _id = 2
            account_name =
            account_type =
                data_set =
                sourceid =
raw_contact_is_read_only = 0
                 version = 2
                   dirty = 1
                 deleted = 0
              contact_id = 2
        aggregation_mode = 0
      aggregation_needed = 0
         custom_ringtone =
       send_to_voicemail = 0
         times_contacted = 0
     last_time_contacted =
                 starred = 0
            display_name = Larry
        display_name_alt = Larry
     display_name_source = 40
           phonetic_name =
     phonetic_name_style = 3
                sort_key = Larry
            sort_key_alt = Larry
           name_verified = 0
                   sync1 =
                   sync2 =
                   sync3 =
                   sync4 =

                     _id = 3
            account_name =
            account_type =
                data_set =
                sourceid =
raw_contact_is_read_only = 0
                 version = 2
                   dirty = 1
                 deleted = 0
              contact_id = 3
        aggregation_mode = 0
      aggregation_needed = 0
         custom_ringtone =
       send_to_voicemail = 0
         times_contacted = 0
     last_time_contacted =
                 starred = 0
            display_name = Curly
        display_name_alt = Curly
     display_name_source = 40
           phonetic_name =
     phonetic_name_style = 3
                sort_key = Curly
            sort_key_alt = Curly
           name_verified = 0
                   sync1 =
                   sync2 =
                   sync3 =
                   sync4 =

sqlite> .exit     (exit from sqlite3)
# exit            (exit from adb shell)