[FAQ][A] About Contacts Import/Export, Backup/Restore and Data/Sync

Status
Not open for further replies.
Search This thread

E:V:A

Inactive Recognized Developer
Dec 6, 2011
1,447
2,222
-∇ϕ
All About Contacts Import/Export, Backup/Restore and Data/Sync

Last Change: 2011-12-29

Content:

Details about how to backup and restore Contacts on the
Samsung Galaxy S I & II, including data storage info.

Summary:

Backing up and restoring your contacts on the latest Android OS
based phones is a nightmare as long as you do not want to use
automatic sync via Google or Outlook Express. In fact without
paying money for some specialized Application to do it properly
for you, it is a mess. Here I feel Samsung have really failed.
Why there isn't already a built-in simple-to-use contact backup
functionality, that doesn't depend on Google, Kies, Bluetooth
or other net-based service, is way beyond me.

Anyway, here is my attempt to remedy this situation and the
various options you have to backup and restore all your contacts
without
:

a) using Google, Microsoft, Kies or other wireless protocol and
b) paying for specialized applications or software.

To simplify this exercise (a lot) we will assume that we would be
satisfied with backup and restore of only the most essential information.
This means (for me):

- first_name
- last_name
- primary_phone
- primary_email

So let's see how far we can get...

NOTE:

a) All of this tutorial assumes you have a rooted phone and
that you are operating as the root user (su)!

b) AOS phones doesn't generally come with the SQLite3
command-line binary, so this have to be installed manually!

Keywords:

Contacts, Restore, CSV, vCard, SQL,
Backup, Data, Sync, SQLite3, Import, Export, VCF

Content​
- Introduction
- Remounting /system as Read/Write
- Installing the SQLite Command-Line Interpreter
- SQLite3 Install Script
- Contacts Database & Storage-Location:
- Contacts DB Location List
- Direct Manipulation of the Contacts Database

- Other Contact Import / Export Applications

- Help Needed!
Introduction​
Essentially there are only 4 different ways to backup your Android contacts.

1. By using a backup application like Kies or 3rd party one from Android Market.
2. By syncing your contacts to your Google account.
3. By copying the entire "Contacts" database to your PC.
4. By exporting only the relevant data directly on your phone, using SQLite.

In this post I will only talk about the last 2 options, with focus on (4) as it applies to (3) as well.

Remounting /system as Read/Write​
Any manipulation of the system partitions and the root directory require WRITE permission to the file-system. To find out, just use the mount command and look for the "rw" label in the filesystem description of the / and /system partitions. For example:

Code:
# mount 
...
rootfs / rootfs ro,relatime 0 0
/dev/block/mmcblk0p9 /system ext4 ro,relatime,barrier=1,data=ordered 0 0
/dev/block/mmcblk0p10 /data ext4 rw,nosuid,nodev,noatime,...,discard 0 0
/dev/block/mmcblk0p4 /mnt/.lfs j4fs rw,relatime 0 0
/dev/block/vold/179:11 /mnt/sdcard vfat rw,dirsync,...,utf8,...,discard 0 0
...
See the "rw" following "/system ext4" and "/data ext4" above?
Yes? Good, you can skip to the next section!
No? Then you need to remount the filesystem as READ/WRITE.
("rw" = "Read AND Write" and "ro" = "Read Only".)

!! However, this may depend on your model AND on how you connected your phone to USB. You can enter the phone directly without using the "file-transfer" mode in which case you may or may not have RW, but if you select to use "file-transfer" (aka. USB-disk mode) you may only get RO. In some cases the /mnt/sdcard is simply never mounted. Try and test! [*** check this! ***]

The way to get RW, is to remap the "physical" device (disk) location to the system partition you are interested in. The tricky part is knowing the physical device name of your system disk which may not always be the same as indicated from the mount command. If not, try to inspect the boot-log messages with the "motd" command, or inspecting the file "/init.rc" for the details of everything that is mounted upon boot. (Boot-log messages are normally enabled in most stock Kernels, but not necessarily in modded ROM's.)

For the SGS-2 which uses the EXT4 filesystem, I had to use:
Code:
mount -o remount,rw -t ext4 /dev/block/mmcblk0p9 /system
For the SGS-1 when using an RFS based AOS, I had to use:
Code:
mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system
To undo this you just replace "rw" with "ro" in the statements above. Although this is not necessary, since it will be automatically reverted after next reboot, which is recomended anyway after manually editing any system DB.

Installing the SQLite Command-Line Interpreter​
Most phones doesn't come with the SQLite3 command-line interpreter/interface (CLI) installed, unless they are rooted with a heavily modded ROM. The CLI is needed to manually access the SQLite Contacts database. Thus you need to check that it is not already installed and install it manually, if it is not. The binary (sqlite3) is normally located in either /system/bin or /system/xbin. To check if you have it, try the following command:

Code:
find / -iname '*sqlite*'
If it fails to return the files shown below, you need to install the SQLite3 binary AND possibly the relevant library files. You should already have most of the required library (lib*.so) files, as they are constantly used by the system, so look for them first! The files you need are:

/system/bin/sqlite3
/system/lib/libsqlite.so
/system/lib/libsqlite_jni.so


If the binary is compiled from CyanogenMod sources, you will also need the following:

/system/lib/libncurses.so

In the worst case you can go through the exercise to compile the SQLite3 sources for the MIPS32 chipset by yourself. [http://www.sqlite.org/] Also it's worth to note that some custom ROM's now include the latest compiled version. When you have the binary you just put it on your SD card and copy it to the root file system binary directory. Many ROM's and rooted phones use an additional directory /system/xbin where you can put your own binaries. If this directory (and path variable) is not already present, use:

Code:
cp <path_to_your_sqlite3_binary> /system/bin/sqlite3 
chmod 4755 /system/bin/sqlite3
chown root.shell /system/bin/sqlite3
(Note that this version of chown uses a "." in the "user.group" specification.)
Do the same for the library files, if they are missing. You should now be able to run SQLite3:

Code:
# sqlite3 -version
3.6.22
Everything OK? Good!

SQLite3 Install Script​
!!MODIFY FIRST!!

Code:
#!/bin/sh
# squint.sh - sqlite3 install script for rooted Androids
#
#-rwsr-sr-x root  shell    22228 2011-11-10 12:53 /system/bin/su
#-rwsr-xr-x root  shell  1075144 2011-12-15 16:55 /system/xbin/busybox
#-rw-r--r-- root  root    322120 2011-09-14 14:45 /system/lib/libsqlite.so

# Go to your "sqlite" folder on your (internal) SD card:
cd /mnt/sdcard/sqlite/

cp sqlite3 /system/xbin/sqlite3
chmod 4755 /system/xbin/sqlite3
chown root.shell /system/xbin/sqlite3

cp ./lib/libncurses.so /system/lib/libncurses.so
chmod 644 /system/lib/libncurses.so
chown root.root /system/lib/libncurses.so
Contacts Database & Storage-Location​
Apparently the SQLite3 database used to store the Contacts data is located in different parts on different Android models [?] and different Android OS versions. I have not been able to verify this, as I don't have access to these phones, but from from reading various forum posts for other phones this seem to be the case.

The way you can check is by looking for the raw database file on your system. On the phones I have, the location can be found here:

SGS-1 (2.3.6): /dbdata/databases/com.android.providers.contacts/contacts.db
SGS-2 (2.3.4): /data/data/com.android.providers.contacts/databases/contacts2.db

It may be important to notice that the "com.android.providers.contacts" part of this path, is actually referring to the particular "Contacts.apk" application that is installed in your phone. Therefore this location can differ from other versions of that application and thus also your phone model and OS version. In addition note that the /data and /dbdata or "whatever" partitions may be mounted on a different block device, as noted in the first section above. For example, on my SGS-2, I had /data mounted on /dev/block/mmcblk0p10 .


The more intelligent way is to look for all .db files on you system by doing some shell magic:

Code:
find / -iname '*.db'
But today, almost every application is using a database to save information, so to narrow down the long list, filter your search with "ontact":

Code:
find /system -iname '*.db' |grep ontact
Contacts DB Location List​
GT-I9000 (2.3.6): /dbdata/databases/com.android.providers.contacts/contacts.db
GT-I9100 (2.3.4): /data/data/com.android.providers.contacts/databases/contacts2.db


[I'd like to keep this list updated with other models and AOS versions, so please let me know what you have.]

Direct Manipulation of the Contacts Database​
In order to make any changes to any information contained in the database, you need to have write permissions set on the the relevant partition(s) as explained above. Remount as needed.

If you for some crazy reason need to replace/restore your entire contact database with another one. (This is strongly discouraged!) You also need to make sure that the one you replace with have both the same ownership (chown) and permissions (chmod) as the original one.

Next, let's open the contacts database:

Code:
# cd /data/data/com.android.providers.contacts/databases
 # sqlite3 contacts2.db
 
SQLite version 3.6.22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"

sqlite> .databases

.databases
seq  name             file
---  ---------------  ----------------------------------------------------------
0    main             /data/data/com.android.providers.contacts/databases/contacts2.db

sqlite> .tables

.tables
_sync_state                       settings
_sync_state_metadata              speed_dial
accounts                          status_updates
activities                        v1_settings
agg_exceptions                    view_contacts
android_metadata                  view_contacts_restricted
calls                             view_data
contact_entities_view             view_data_restricted
contact_entities_view_restricted  view_groups
contacts                          view_raw_contacts
data                              view_raw_contacts_restricted
groups                            view_v1_contact_methods
mimetypes                         view_v1_extensions
name_lookup                       view_v1_group_membership
nickname_lookup                   view_v1_groups
packages                          view_v1_organizations
phone_lookup                      view_v1_people
properties                        view_v1_phones
raw_contacts                      view_v1_photos

sqlite> SELECT _id,name, number FROM view_v1_phones;

[...]
To output the result as a CSV table to a file called "my_contacts_db.csv" with the first row containing the (column) headers and bailing out on errors, do this:

Code:
sqlite> .bail ON
sqlite> .headers ON
sqlite> .mode csv
sqlite> .output my_contacts_db.csv
sqlite> SELECT _id,name, number FROM view_v1_phones;
sqlite> .output stdout
sqlite> .quit
As you can see, if we had known the precise query statements we would have already been done with our export. But the Contacts.db tables are rather complex for our purposes of simply extracting the most relevant contact information. I lazily gave up after trying some basic SQL select statements...

[So yeah, please send me a good SQL statement for selecting and printing only the basics as a CSV!]

The AOS java API for using the contacts are MUCH simpler to use than the needed SQLite statements, for doing the extractions/replacements. But then you also need to write the program.

http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/

There are many such programs out there, but very few who does the job well. (Everybody want to make money!) So try some Google-Fu on sites such as:

http://www.sourceforge.com/
http://code.google.com/
http://www.github.com/
http://www.stackexchange.com/
http://grepcode.com/

and use the keywords in the top of this document.

Other Contact Import / Export Applications​
But I'm not a Java Programmer. (What?!!) So instead I had a look at the most simple applications for exporting and importing you contacts. Being reasonably paranoid as a person, I did not trust huge multifunction backup-applications, knowing that they are full of adware, spyware and other "things" we may not like. With these limits I knew that any such application should not be much larger than 100 kilobytes.

I found 4 small applications (and one very large) on the Market that I tested:
1. Contacts CSV Export (93 K)
https://market.android.com/details?id=dutchandroid.contacts.export
(contacts.export-1060-1.060.apk, dutchandroid.contacts.export)

2. Contacts Backup Trial (168K)
https://market.android.com/details?id=com.saturnat.android.contactsbackup.trial
(contactsbackup.trial-34-2.9.5.apk, com.saturnat.android.contactsbackup.trial)

3. Import Contacts (56 K)
https://market.android.com/details?id=org.waxworlds.edam.importcontacts
(import-contacts_1.1.apk, org.waxworlds.edam.importcontacts)

4. Offline Contacts Sync (27 K)
https://market.android.com/details?id=com.beforemadness
(offlineContactsSync.apk, com.beforemadness)

5. GO Contacts EX (2.7 M!)
https://market.android.com/details?id=com.jbapps.contactpro
(1) Is a flexible lightweight CSV contacts exporter. You can manually choose CSV delimiters (",;TAB" etc) or choose a standard (Mozilla, Hotmail and Outlook etc.) CSV format.

(2) $$$, Is buggy with a no-sense UI, doesn't catch Email addresses, don't let you choose filename, and contain trackers from Facebook and Mixpanel. Limited trial to 10 contacts and 3 days before expiration.

(3) Only imports vCard (.vcf) files. One-by-one or scan a directory. Ask's what to do (skip,merge,replace) with duplicates.

(4) No-sense UI. Has export function but it is not working. And import is only working partially.

(5) Exports all contacts to a single vCard file, but missed my Emails. Nice merge and batch-delete functionality. Nice interface, and works. However, this app is huge, and god knows what other bloat junk it contains.


Help Needed!​
If you have any detailed knowledge about where and how the relevant contact data is stored on your device, please let me know and include details such as:

- Your phone model number
- Your general Android OS version [Settings --> About Phone ]
- Your phone firmware version (PDA / PHONE / CSC) ["dial" --> *#1234#]
- The precise location of the contacts database file

E.g.

GT-I9100, Gingerbread 2.3.4, XWKI4/XXKI1/OXX KI2 (SEB)
/dbdata/databases/com.android.providers.contacts/contacts.db

If you have already been poking around your OS it would also be helpful to know:

- The version and type of database used (SQLite3 etc.)
- The particular DB tables used to obtain:

+ First Name
+ Last Name
+ Phone Number "1"
+ Email "1"

Here I have written "1" to signify the primary number used. I have refrained from using more details as this, as I consider this minimal but sufficient for backing up and restoring my contacts.


Some useful SQLite queries​
Code:
sqlite3 -csv contacts2.db "SELECT _id,name,primary_email,number FROM view_v1_phones;"
sqlite3 -csv contacts2.db "SELECT _id,mimetype_id,raw_contact_id,data1 FROM data;"
sqlite3 -csv contacts2.db "SELECT _id,display_name FROM raw_contacts WHERE account_type='vnd.sec.contact.phone';"
sqlite3 -csv contacts2.db "SELECT data1 FROM data WHERE raw_contact_id=112 AND mimetype_id=5;"

sqlite3 -csv -header contacts2.db "SELECT _id,package_id,mimetype_id,raw_contact_id,is_primary,is_super_primary,data_version,data1,data2,data3 FROM data WHERE raw_contact_id=21;"
sqlite3 -csv -header contacts2.db "SELECT _id,package_id,mimetype_id,raw_contact_id,is_primary,is_super_primary,data_version,data1,data2,data3 FROM data WHERE is_primary=0 AND data_version=0;"

#SELECT data1 FROM data WHERE raw_contact_id=140 AND mimetype_id=1;
#SELECT data1 FROM data WHERE raw_contact_id=140 AND mimetype_id=5 AND data2=2;
#SELECT data1 FROM data WHERE raw_contact_id=112 AND mimetype_id=5 AND data2=7;
#SELECT data1 FROM data WHERE raw_contact_id=112 AND mimetype_id=5 AND data2=1;


If this helped you in any way, please hit the THANK YOU button!
 
Last edited:

E:V:A

Inactive Recognized Developer
Dec 6, 2011
1,447
2,222
-∇ϕ
The Android contacts-database extraction script


This script is based on one that I found for extracting Facebook contacts into a CSV file, here: http://xdaforums.com/showthread.php?t=1170765 I then modified and improved this script to suit my own purposes, but I am very grateful to whoever created it in the first place. (There is no reliable reference to the origin of this.)

However, there are two very useful things I found and included in this script.

1) You can extract contacts from other applications using that same database file, by modifying the "java-path-identifyer" accordingly.

Code:
Specify the application used for organizing the data:
-----------------------------------------------------------
vnd.sec.contact.phone           - Default Android phone book
com.facebook.auth.login         - Facebook contacts
2) The sed pipe used to replace carrige-returns (new-lines, \n) from any output stream, with a comma ",".

Code:
sed ':a;N;$!ba;s/\n/,/g'
Explanation:

  1. Create and label a register via :a
  2. Append the current and next line to the register via N
  3. If we are before the last line, branch (b) to the created
    register (a) with $!ba ( "$!" means not to do it on the
    last line, as there should be one final newline).
  4. Use global match (/g) to substitute (s) every newline (\n)
    with a comma on the pattern space, which is the contents
    of the (a) register. (I.e. The whole input stream.)

Here is the script:

Code:
#!/bin/sh
# contex.sh - Extracts the Name,Phone,Email from the Android Contact Database
#==============================================================================
# Last Change:     2011-12-29
# Name:            Android DB Contacts Exporter
#
# Description:  Extracts contacts from the Android contacts database 
#               to a CSV file for import into other contact list tools.
#
# Usage:    ./contex.sh <output_filename> &
# 
# How to use:  
#
# 1. Copy your Android Contacts Database to your computer:
#    adb pull /data/data/com.android.providers.contacts/databases/contacts2.db ./
# 2. Run this file from the same directory as contacts2.db
#     ./contex.sh output_filename.csv
#
# System Requirements:  sh, sqlite3, sed
# 
# Additional Notes:
#
#    1. This script is very slow, please be patient!
#
#    2. The sed sequence to replace the end on line (\n) with a comma (,) 
#       from a list, but avoiding \n on the last line is:
# 
#       sed ':a;N;$!ba;s/\n/,/g'
#       --- OR ---  
#       sed '{:q;N;s/\n//g;t q}'
# 
#    3. Only numbers that are of the types: HOME, MOBILE, WORK and OTHER 
#       get extracted in this script. These types are specified by the 
#    "data2" field in the "data" table.
# 
#==============================================================================
echo; echo "Android contacts backup script started with PID: $$ "; echo;
DATE=`date +%G%m%d`

#----------------------------------------------------------
# Input and Output files:
#----------------------------------------------------------
CONTACTSDB="./contacts2.db"
if [ ! -f "$CONTACTSDB" ]
then
    echo "Contacts Database file: $CONTACTSDB not found. Exiting."; echo
    exit 1
fi

OUTFILE=$1
if [ -z "$OUTFILE" ]
then
    OUTFILE="contex_backup_${DATE}.csv"
fi
echo "Contacts backup file: $OUTFILE "; echo

if [ -f "$OUTFILE" ]
then
    echo "Contacts Backup file: $OUTFILE already exist. Exiting."; echo
    exit 1
fi

#----------------------------------------------------------
# Write the Table Header:
#----------------------------------------------------------
echo "First Name,Last Name,E-mail,Mobile Phone,Home Phone,Work Phone,Other Phone" >> $OUTFILE

#----------------------------------------------------------
# Setting up SQLite command and mode:
#----------------------------------------------------------
#CMD="sqlite3 -bail -noheader -csv" 
CMD="sqlite3";

#----------------------------------------------------------
# Get the raw IDs of all contacts:
#----------------------------------------------------------
RAWID_LIST=`$CMD $CONTACTSDB "SELECT _id from raw_contacts WHERE account_type='vnd.sec.contact.phone'"`
#echo "$RAWID_LIST"

#----------------------------------------------------------
# Loop through the IDs and extract all necessary data
#----------------------------------------------------------
for RAWID in $RAWID_LIST; do

    # Get the First_Name and Last_Name in one query:
    # NOTE: Need to remove occasional CTRL-M's with sed.
    NAME=`$CMD -csv $CONTACTSDB "SELECT data2, data3 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=6;" | sed ':a;N;$!ba;s/\n//g'`

    # Get the Email address:
    # NOTE: This will simply append multiple email adresses to one long string, with ";" separator!
    EMAIL=`$CMD $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=1;" | sed ':a;N;$!ba;s/\n/;/g'`

    # Here we get the "Home", "Mobile", Work" and "Other" (labeled) phone numbers.
    # NOTE: Each of these may return multiple responses, so we need to convert new-lines (\n) to ",".
    HPHONE=`$CMD $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=5 AND data2=1;" | sed ':a;N;$!ba;s/\n/,/g'`;
    MPHONE=`$CMD $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=5 AND data2=2;" | sed ':a;N;$!ba;s/\n/,/g'`;
    WPHONE=`$CMD $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=5 AND data2=3;" | sed ':a;N;$!ba;s/\n/,/g'`;
    OPHONE=`$CMD $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=5 AND data2=7;" | sed ':a;N;$!ba;s/\n/,/g'`;
    PHONES="$MPHONE,$HPHONE,$WPHONE,$OPHONE";

    # If at least one main field is found then write the CSV line
    if [ -n "$NAME" ] || [ -n "$EMAIL" ] || [ -n "$PHONES" ]; then
        #echo "ID: $RAWID"
        echo -n "$NAME,$EMAIL,$PHONES" >> $OUTFILE
        echo >> $OUTFILE
    fi

done
echo 'Done.'
NOW YOU CAN HIT THE THANK YOU BUTTON!
 
Last edited:

webified

Member
Oct 14, 2009
5
4
Thanks for this write up. I found my stuck after upgrading a friends phone from 2.2 firmware to CM10 with 4.1 firmware. We made a full backup with TitaniumBackup but it would import the contacts properly. I pulled the contacts2.db from the backup file and used your script to get the data out.

I did have to make some changes to the mimetypes in the SQLs but apart from that everything worked perfectly. I think the mimetypes may have changed between android versions.

The modified portion of the script:
Code:
    # Get the First_Name and Last_Name in one query:
    # NOTE: Need to remove occasional CTRL-M's with sed.
    NAME=`$CMD -csv $CONTACTSDB "SELECT data2, data3 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=5;" | sed ':a;N;$!ba;s/\n//g'`

    # Get the Email address:
    # NOTE: This will simply append multiple email adresses to one long string, with ";" separator!
    EMAIL=`$CMD $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=1;" | sed ':a;N;$!ba;s/\n/;/g'`

    # Here we get the "Home", "Mobile", Work" and "Other" (labeled) phone numbers.
    # NOTE: Each of these may return multiple responses, so we need to convert new-lines (\n) to ",".
    HPHONE=`$CMD $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=6 AND data2=1;" | sed ':a;N;$!ba;s/\n/,/g'`;
    MPHONE=`$CMD $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=6 AND data2=2;" | sed ':a;N;$!ba;s/\n/,/g'`;
    WPHONE=`$CMD $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=6 AND data2=3;" | sed ':a;N;$!ba;s/\n/,/g'`;
    OPHONE=`$CMD $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=6 AND data2=7;" | sed ':a;N;$!ba;s/\n/,/g'`;
    PHONES="$MPHONE,$HPHONE,$WPHONE,$OPHONE";

Thanks so much for your post.
 

CoinCoin88

Senior Member
Sep 15, 2011
307
84
Paris
OnePlus 8 Pro
I can't thank you enough for this information!
My S2 died on me a month back, and the only backup of my contacts (which are not synced on Gmail) were an old TB backup from April 2012. At least thanks to this knowledge I know what and where to look for!

Thanks a lot ;)
 
  • Like
Reactions: E:V:A

SeaFargo

Senior Member
Feb 24, 2011
324
59
Rome
rvd.it
Help request with command

Hello.

I am trying to convert a recovered contacts database from contacts2.db to Google compatible cvs file with sqlite3.exe.

The recovered database came from a Samsung N7000 and it is composed as follow in the table data:

SELECT "_id", "package_id", "mimetype_id", "raw_contact_id", "is_primary", "is_super_primary", "data_version", "data1", "data2", "data3", "data4", "data5", "data6", "data7", "data8", "data9", "data10", "data11", "data12", "data13", "data14", "data15", "data_sync1", "data_sync2", "data_sync3", "data_sync4", "is_read_only" FROM "data";

There are 2 lines for a single contact. mimetype_id 6 is the line of the name in data1; mimetype_id 5 is the line of the number in data1

Is there a command to properly export that database? I have no skills in programming and scripting.

I have to complete an import of a database to a new android phone from a N7000 old contacts2.db :crying:
 

SeaFargo

Senior Member
Feb 24, 2011
324
59
Rome
rvd.it
Just to say that the code
Code:
SELECT "_id", "package_id", "mimetype_id", "raw_contact_id", "is_primary", "is_super_primary", "data_version", "data1", "data2", "data3", "data4", "data5", "data6", "data7", "data8", "data9", "data10", "data11", "data12", "data13", "data14", "data15", "data_sync1", "data_sync2", "data_sync3", "data_sync4", "is_read_only" FROM "data";
gives something like that:

5406,,5,2335,0,0,0,349563erased for privacy,2,
5407,,6,2335,0,0,0,"federico agronomo",federico,agronomo


And so on...

Since I have no skills in developing and scripting, is there anybody how can help me extracting universal VCF from that N7000 sql contacts2.db specific database? I can send it to you by eMail if you write to me in pvt. I'll apprecciated.

I did a lot of tests, but I can not create a proper query or run a proper script.

I was able only to extract the database in a way like the one mentioned. I have quite 1000 contacts :crying:

Thank you in any way for help.

Kind regards.
 
Status
Not open for further replies.

Top Liked Posts

  • There are no posts matching your filters.
  • 9
    All About Contacts Import/Export, Backup/Restore and Data/Sync

    Last Change: 2011-12-29

    Content:

    Details about how to backup and restore Contacts on the
    Samsung Galaxy S I & II, including data storage info.

    Summary:

    Backing up and restoring your contacts on the latest Android OS
    based phones is a nightmare as long as you do not want to use
    automatic sync via Google or Outlook Express. In fact without
    paying money for some specialized Application to do it properly
    for you, it is a mess. Here I feel Samsung have really failed.
    Why there isn't already a built-in simple-to-use contact backup
    functionality, that doesn't depend on Google, Kies, Bluetooth
    or other net-based service, is way beyond me.

    Anyway, here is my attempt to remedy this situation and the
    various options you have to backup and restore all your contacts
    without
    :

    a) using Google, Microsoft, Kies or other wireless protocol and
    b) paying for specialized applications or software.

    To simplify this exercise (a lot) we will assume that we would be
    satisfied with backup and restore of only the most essential information.
    This means (for me):

    - first_name
    - last_name
    - primary_phone
    - primary_email

    So let's see how far we can get...

    NOTE:

    a) All of this tutorial assumes you have a rooted phone and
    that you are operating as the root user (su)!

    b) AOS phones doesn't generally come with the SQLite3
    command-line binary, so this have to be installed manually!

    Keywords:

    Contacts, Restore, CSV, vCard, SQL,
    Backup, Data, Sync, SQLite3, Import, Export, VCF

    Content​
    - Introduction
    - Remounting /system as Read/Write
    - Installing the SQLite Command-Line Interpreter
    - SQLite3 Install Script
    - Contacts Database & Storage-Location:
    - Contacts DB Location List
    - Direct Manipulation of the Contacts Database

    - Other Contact Import / Export Applications

    - Help Needed!
    Introduction​
    Essentially there are only 4 different ways to backup your Android contacts.

    1. By using a backup application like Kies or 3rd party one from Android Market.
    2. By syncing your contacts to your Google account.
    3. By copying the entire "Contacts" database to your PC.
    4. By exporting only the relevant data directly on your phone, using SQLite.

    In this post I will only talk about the last 2 options, with focus on (4) as it applies to (3) as well.

    Remounting /system as Read/Write​
    Any manipulation of the system partitions and the root directory require WRITE permission to the file-system. To find out, just use the mount command and look for the "rw" label in the filesystem description of the / and /system partitions. For example:

    Code:
    # mount 
    ...
    rootfs / rootfs ro,relatime 0 0
    /dev/block/mmcblk0p9 /system ext4 ro,relatime,barrier=1,data=ordered 0 0
    /dev/block/mmcblk0p10 /data ext4 rw,nosuid,nodev,noatime,...,discard 0 0
    /dev/block/mmcblk0p4 /mnt/.lfs j4fs rw,relatime 0 0
    /dev/block/vold/179:11 /mnt/sdcard vfat rw,dirsync,...,utf8,...,discard 0 0
    ...
    See the "rw" following "/system ext4" and "/data ext4" above?
    Yes? Good, you can skip to the next section!
    No? Then you need to remount the filesystem as READ/WRITE.
    ("rw" = "Read AND Write" and "ro" = "Read Only".)

    !! However, this may depend on your model AND on how you connected your phone to USB. You can enter the phone directly without using the "file-transfer" mode in which case you may or may not have RW, but if you select to use "file-transfer" (aka. USB-disk mode) you may only get RO. In some cases the /mnt/sdcard is simply never mounted. Try and test! [*** check this! ***]

    The way to get RW, is to remap the "physical" device (disk) location to the system partition you are interested in. The tricky part is knowing the physical device name of your system disk which may not always be the same as indicated from the mount command. If not, try to inspect the boot-log messages with the "motd" command, or inspecting the file "/init.rc" for the details of everything that is mounted upon boot. (Boot-log messages are normally enabled in most stock Kernels, but not necessarily in modded ROM's.)

    For the SGS-2 which uses the EXT4 filesystem, I had to use:
    Code:
    mount -o remount,rw -t ext4 /dev/block/mmcblk0p9 /system
    For the SGS-1 when using an RFS based AOS, I had to use:
    Code:
    mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system
    To undo this you just replace "rw" with "ro" in the statements above. Although this is not necessary, since it will be automatically reverted after next reboot, which is recomended anyway after manually editing any system DB.

    Installing the SQLite Command-Line Interpreter​
    Most phones doesn't come with the SQLite3 command-line interpreter/interface (CLI) installed, unless they are rooted with a heavily modded ROM. The CLI is needed to manually access the SQLite Contacts database. Thus you need to check that it is not already installed and install it manually, if it is not. The binary (sqlite3) is normally located in either /system/bin or /system/xbin. To check if you have it, try the following command:

    Code:
    find / -iname '*sqlite*'
    If it fails to return the files shown below, you need to install the SQLite3 binary AND possibly the relevant library files. You should already have most of the required library (lib*.so) files, as they are constantly used by the system, so look for them first! The files you need are:

    /system/bin/sqlite3
    /system/lib/libsqlite.so
    /system/lib/libsqlite_jni.so


    If the binary is compiled from CyanogenMod sources, you will also need the following:

    /system/lib/libncurses.so

    In the worst case you can go through the exercise to compile the SQLite3 sources for the MIPS32 chipset by yourself. [http://www.sqlite.org/] Also it's worth to note that some custom ROM's now include the latest compiled version. When you have the binary you just put it on your SD card and copy it to the root file system binary directory. Many ROM's and rooted phones use an additional directory /system/xbin where you can put your own binaries. If this directory (and path variable) is not already present, use:

    Code:
    cp <path_to_your_sqlite3_binary> /system/bin/sqlite3 
    chmod 4755 /system/bin/sqlite3
    chown root.shell /system/bin/sqlite3
    (Note that this version of chown uses a "." in the "user.group" specification.)
    Do the same for the library files, if they are missing. You should now be able to run SQLite3:

    Code:
    # sqlite3 -version
    3.6.22
    Everything OK? Good!

    SQLite3 Install Script​
    !!MODIFY FIRST!!

    Code:
    #!/bin/sh
    # squint.sh - sqlite3 install script for rooted Androids
    #
    #-rwsr-sr-x root  shell    22228 2011-11-10 12:53 /system/bin/su
    #-rwsr-xr-x root  shell  1075144 2011-12-15 16:55 /system/xbin/busybox
    #-rw-r--r-- root  root    322120 2011-09-14 14:45 /system/lib/libsqlite.so
    
    # Go to your "sqlite" folder on your (internal) SD card:
    cd /mnt/sdcard/sqlite/
    
    cp sqlite3 /system/xbin/sqlite3
    chmod 4755 /system/xbin/sqlite3
    chown root.shell /system/xbin/sqlite3
    
    cp ./lib/libncurses.so /system/lib/libncurses.so
    chmod 644 /system/lib/libncurses.so
    chown root.root /system/lib/libncurses.so
    Contacts Database & Storage-Location​
    Apparently the SQLite3 database used to store the Contacts data is located in different parts on different Android models [?] and different Android OS versions. I have not been able to verify this, as I don't have access to these phones, but from from reading various forum posts for other phones this seem to be the case.

    The way you can check is by looking for the raw database file on your system. On the phones I have, the location can be found here:

    SGS-1 (2.3.6): /dbdata/databases/com.android.providers.contacts/contacts.db
    SGS-2 (2.3.4): /data/data/com.android.providers.contacts/databases/contacts2.db

    It may be important to notice that the "com.android.providers.contacts" part of this path, is actually referring to the particular "Contacts.apk" application that is installed in your phone. Therefore this location can differ from other versions of that application and thus also your phone model and OS version. In addition note that the /data and /dbdata or "whatever" partitions may be mounted on a different block device, as noted in the first section above. For example, on my SGS-2, I had /data mounted on /dev/block/mmcblk0p10 .


    The more intelligent way is to look for all .db files on you system by doing some shell magic:

    Code:
    find / -iname '*.db'
    But today, almost every application is using a database to save information, so to narrow down the long list, filter your search with "ontact":

    Code:
    find /system -iname '*.db' |grep ontact
    Contacts DB Location List​
    GT-I9000 (2.3.6): /dbdata/databases/com.android.providers.contacts/contacts.db
    GT-I9100 (2.3.4): /data/data/com.android.providers.contacts/databases/contacts2.db


    [I'd like to keep this list updated with other models and AOS versions, so please let me know what you have.]

    Direct Manipulation of the Contacts Database​
    In order to make any changes to any information contained in the database, you need to have write permissions set on the the relevant partition(s) as explained above. Remount as needed.

    If you for some crazy reason need to replace/restore your entire contact database with another one. (This is strongly discouraged!) You also need to make sure that the one you replace with have both the same ownership (chown) and permissions (chmod) as the original one.

    Next, let's open the contacts database:

    Code:
    # cd /data/data/com.android.providers.contacts/databases
     # sqlite3 contacts2.db
     
    SQLite version 3.6.22
    Enter ".help" for instructions
    Enter SQL statements terminated with a ";"
    
    sqlite> .databases
    
    .databases
    seq  name             file
    ---  ---------------  ----------------------------------------------------------
    0    main             /data/data/com.android.providers.contacts/databases/contacts2.db
    
    sqlite> .tables
    
    .tables
    _sync_state                       settings
    _sync_state_metadata              speed_dial
    accounts                          status_updates
    activities                        v1_settings
    agg_exceptions                    view_contacts
    android_metadata                  view_contacts_restricted
    calls                             view_data
    contact_entities_view             view_data_restricted
    contact_entities_view_restricted  view_groups
    contacts                          view_raw_contacts
    data                              view_raw_contacts_restricted
    groups                            view_v1_contact_methods
    mimetypes                         view_v1_extensions
    name_lookup                       view_v1_group_membership
    nickname_lookup                   view_v1_groups
    packages                          view_v1_organizations
    phone_lookup                      view_v1_people
    properties                        view_v1_phones
    raw_contacts                      view_v1_photos
    
    sqlite> SELECT _id,name, number FROM view_v1_phones;
    
    [...]
    To output the result as a CSV table to a file called "my_contacts_db.csv" with the first row containing the (column) headers and bailing out on errors, do this:

    Code:
    sqlite> .bail ON
    sqlite> .headers ON
    sqlite> .mode csv
    sqlite> .output my_contacts_db.csv
    sqlite> SELECT _id,name, number FROM view_v1_phones;
    sqlite> .output stdout
    sqlite> .quit
    As you can see, if we had known the precise query statements we would have already been done with our export. But the Contacts.db tables are rather complex for our purposes of simply extracting the most relevant contact information. I lazily gave up after trying some basic SQL select statements...

    [So yeah, please send me a good SQL statement for selecting and printing only the basics as a CSV!]

    The AOS java API for using the contacts are MUCH simpler to use than the needed SQLite statements, for doing the extractions/replacements. But then you also need to write the program.

    http://www.higherpass.com/Android/Tutorials/Working-With-Android-Contacts/

    There are many such programs out there, but very few who does the job well. (Everybody want to make money!) So try some Google-Fu on sites such as:

    http://www.sourceforge.com/
    http://code.google.com/
    http://www.github.com/
    http://www.stackexchange.com/
    http://grepcode.com/

    and use the keywords in the top of this document.

    Other Contact Import / Export Applications​
    But I'm not a Java Programmer. (What?!!) So instead I had a look at the most simple applications for exporting and importing you contacts. Being reasonably paranoid as a person, I did not trust huge multifunction backup-applications, knowing that they are full of adware, spyware and other "things" we may not like. With these limits I knew that any such application should not be much larger than 100 kilobytes.

    I found 4 small applications (and one very large) on the Market that I tested:
    1. Contacts CSV Export (93 K)
    https://market.android.com/details?id=dutchandroid.contacts.export
    (contacts.export-1060-1.060.apk, dutchandroid.contacts.export)

    2. Contacts Backup Trial (168K)
    https://market.android.com/details?id=com.saturnat.android.contactsbackup.trial
    (contactsbackup.trial-34-2.9.5.apk, com.saturnat.android.contactsbackup.trial)

    3. Import Contacts (56 K)
    https://market.android.com/details?id=org.waxworlds.edam.importcontacts
    (import-contacts_1.1.apk, org.waxworlds.edam.importcontacts)

    4. Offline Contacts Sync (27 K)
    https://market.android.com/details?id=com.beforemadness
    (offlineContactsSync.apk, com.beforemadness)

    5. GO Contacts EX (2.7 M!)
    https://market.android.com/details?id=com.jbapps.contactpro
    (1) Is a flexible lightweight CSV contacts exporter. You can manually choose CSV delimiters (",;TAB" etc) or choose a standard (Mozilla, Hotmail and Outlook etc.) CSV format.

    (2) $$$, Is buggy with a no-sense UI, doesn't catch Email addresses, don't let you choose filename, and contain trackers from Facebook and Mixpanel. Limited trial to 10 contacts and 3 days before expiration.

    (3) Only imports vCard (.vcf) files. One-by-one or scan a directory. Ask's what to do (skip,merge,replace) with duplicates.

    (4) No-sense UI. Has export function but it is not working. And import is only working partially.

    (5) Exports all contacts to a single vCard file, but missed my Emails. Nice merge and batch-delete functionality. Nice interface, and works. However, this app is huge, and god knows what other bloat junk it contains.


    Help Needed!​
    If you have any detailed knowledge about where and how the relevant contact data is stored on your device, please let me know and include details such as:

    - Your phone model number
    - Your general Android OS version [Settings --> About Phone ]
    - Your phone firmware version (PDA / PHONE / CSC) ["dial" --> *#1234#]
    - The precise location of the contacts database file

    E.g.

    GT-I9100, Gingerbread 2.3.4, XWKI4/XXKI1/OXX KI2 (SEB)
    /dbdata/databases/com.android.providers.contacts/contacts.db

    If you have already been poking around your OS it would also be helpful to know:

    - The version and type of database used (SQLite3 etc.)
    - The particular DB tables used to obtain:

    + First Name
    + Last Name
    + Phone Number "1"
    + Email "1"

    Here I have written "1" to signify the primary number used. I have refrained from using more details as this, as I consider this minimal but sufficient for backing up and restoring my contacts.


    Some useful SQLite queries​
    Code:
    sqlite3 -csv contacts2.db "SELECT _id,name,primary_email,number FROM view_v1_phones;"
    sqlite3 -csv contacts2.db "SELECT _id,mimetype_id,raw_contact_id,data1 FROM data;"
    sqlite3 -csv contacts2.db "SELECT _id,display_name FROM raw_contacts WHERE account_type='vnd.sec.contact.phone';"
    sqlite3 -csv contacts2.db "SELECT data1 FROM data WHERE raw_contact_id=112 AND mimetype_id=5;"
    
    sqlite3 -csv -header contacts2.db "SELECT _id,package_id,mimetype_id,raw_contact_id,is_primary,is_super_primary,data_version,data1,data2,data3 FROM data WHERE raw_contact_id=21;"
    sqlite3 -csv -header contacts2.db "SELECT _id,package_id,mimetype_id,raw_contact_id,is_primary,is_super_primary,data_version,data1,data2,data3 FROM data WHERE is_primary=0 AND data_version=0;"
    
    #SELECT data1 FROM data WHERE raw_contact_id=140 AND mimetype_id=1;
    #SELECT data1 FROM data WHERE raw_contact_id=140 AND mimetype_id=5 AND data2=2;
    #SELECT data1 FROM data WHERE raw_contact_id=112 AND mimetype_id=5 AND data2=7;
    #SELECT data1 FROM data WHERE raw_contact_id=112 AND mimetype_id=5 AND data2=1;


    If this helped you in any way, please hit the THANK YOU button!
    4
    The Android contacts-database extraction script


    This script is based on one that I found for extracting Facebook contacts into a CSV file, here: http://xdaforums.com/showthread.php?t=1170765 I then modified and improved this script to suit my own purposes, but I am very grateful to whoever created it in the first place. (There is no reliable reference to the origin of this.)

    However, there are two very useful things I found and included in this script.

    1) You can extract contacts from other applications using that same database file, by modifying the "java-path-identifyer" accordingly.

    Code:
    Specify the application used for organizing the data:
    -----------------------------------------------------------
    vnd.sec.contact.phone           - Default Android phone book
    com.facebook.auth.login         - Facebook contacts
    2) The sed pipe used to replace carrige-returns (new-lines, \n) from any output stream, with a comma ",".

    Code:
    sed ':a;N;$!ba;s/\n/,/g'
    Explanation:

    1. Create and label a register via :a
    2. Append the current and next line to the register via N
    3. If we are before the last line, branch (b) to the created
      register (a) with $!ba ( "$!" means not to do it on the
      last line, as there should be one final newline).
    4. Use global match (/g) to substitute (s) every newline (\n)
      with a comma on the pattern space, which is the contents
      of the (a) register. (I.e. The whole input stream.)

    Here is the script:

    Code:
    #!/bin/sh
    # contex.sh - Extracts the Name,Phone,Email from the Android Contact Database
    #==============================================================================
    # Last Change:     2011-12-29
    # Name:            Android DB Contacts Exporter
    #
    # Description:  Extracts contacts from the Android contacts database 
    #               to a CSV file for import into other contact list tools.
    #
    # Usage:    ./contex.sh <output_filename> &
    # 
    # How to use:  
    #
    # 1. Copy your Android Contacts Database to your computer:
    #    adb pull /data/data/com.android.providers.contacts/databases/contacts2.db ./
    # 2. Run this file from the same directory as contacts2.db
    #     ./contex.sh output_filename.csv
    #
    # System Requirements:  sh, sqlite3, sed
    # 
    # Additional Notes:
    #
    #    1. This script is very slow, please be patient!
    #
    #    2. The sed sequence to replace the end on line (\n) with a comma (,) 
    #       from a list, but avoiding \n on the last line is:
    # 
    #       sed ':a;N;$!ba;s/\n/,/g'
    #       --- OR ---  
    #       sed '{:q;N;s/\n//g;t q}'
    # 
    #    3. Only numbers that are of the types: HOME, MOBILE, WORK and OTHER 
    #       get extracted in this script. These types are specified by the 
    #    "data2" field in the "data" table.
    # 
    #==============================================================================
    echo; echo "Android contacts backup script started with PID: $$ "; echo;
    DATE=`date +%G%m%d`
    
    #----------------------------------------------------------
    # Input and Output files:
    #----------------------------------------------------------
    CONTACTSDB="./contacts2.db"
    if [ ! -f "$CONTACTSDB" ]
    then
        echo "Contacts Database file: $CONTACTSDB not found. Exiting."; echo
        exit 1
    fi
    
    OUTFILE=$1
    if [ -z "$OUTFILE" ]
    then
        OUTFILE="contex_backup_${DATE}.csv"
    fi
    echo "Contacts backup file: $OUTFILE "; echo
    
    if [ -f "$OUTFILE" ]
    then
        echo "Contacts Backup file: $OUTFILE already exist. Exiting."; echo
        exit 1
    fi
    
    #----------------------------------------------------------
    # Write the Table Header:
    #----------------------------------------------------------
    echo "First Name,Last Name,E-mail,Mobile Phone,Home Phone,Work Phone,Other Phone" >> $OUTFILE
    
    #----------------------------------------------------------
    # Setting up SQLite command and mode:
    #----------------------------------------------------------
    #CMD="sqlite3 -bail -noheader -csv" 
    CMD="sqlite3";
    
    #----------------------------------------------------------
    # Get the raw IDs of all contacts:
    #----------------------------------------------------------
    RAWID_LIST=`$CMD $CONTACTSDB "SELECT _id from raw_contacts WHERE account_type='vnd.sec.contact.phone'"`
    #echo "$RAWID_LIST"
    
    #----------------------------------------------------------
    # Loop through the IDs and extract all necessary data
    #----------------------------------------------------------
    for RAWID in $RAWID_LIST; do
    
        # Get the First_Name and Last_Name in one query:
        # NOTE: Need to remove occasional CTRL-M's with sed.
        NAME=`$CMD -csv $CONTACTSDB "SELECT data2, data3 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=6;" | sed ':a;N;$!ba;s/\n//g'`
    
        # Get the Email address:
        # NOTE: This will simply append multiple email adresses to one long string, with ";" separator!
        EMAIL=`$CMD $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=1;" | sed ':a;N;$!ba;s/\n/;/g'`
    
        # Here we get the "Home", "Mobile", Work" and "Other" (labeled) phone numbers.
        # NOTE: Each of these may return multiple responses, so we need to convert new-lines (\n) to ",".
        HPHONE=`$CMD $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=5 AND data2=1;" | sed ':a;N;$!ba;s/\n/,/g'`;
        MPHONE=`$CMD $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=5 AND data2=2;" | sed ':a;N;$!ba;s/\n/,/g'`;
        WPHONE=`$CMD $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=5 AND data2=3;" | sed ':a;N;$!ba;s/\n/,/g'`;
        OPHONE=`$CMD $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=5 AND data2=7;" | sed ':a;N;$!ba;s/\n/,/g'`;
        PHONES="$MPHONE,$HPHONE,$WPHONE,$OPHONE";
    
        # If at least one main field is found then write the CSV line
        if [ -n "$NAME" ] || [ -n "$EMAIL" ] || [ -n "$PHONES" ]; then
            #echo "ID: $RAWID"
            echo -n "$NAME,$EMAIL,$PHONES" >> $OUTFILE
            echo >> $OUTFILE
        fi
    
    done
    echo 'Done.'
    NOW YOU CAN HIT THE THANK YOU BUTTON!
    2
    Thanks for this write up. I found my stuck after upgrading a friends phone from 2.2 firmware to CM10 with 4.1 firmware. We made a full backup with TitaniumBackup but it would import the contacts properly. I pulled the contacts2.db from the backup file and used your script to get the data out.

    I did have to make some changes to the mimetypes in the SQLs but apart from that everything worked perfectly. I think the mimetypes may have changed between android versions.

    The modified portion of the script:
    Code:
        # Get the First_Name and Last_Name in one query:
        # NOTE: Need to remove occasional CTRL-M's with sed.
        NAME=`$CMD -csv $CONTACTSDB "SELECT data2, data3 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=5;" | sed ':a;N;$!ba;s/\n//g'`
    
        # Get the Email address:
        # NOTE: This will simply append multiple email adresses to one long string, with ";" separator!
        EMAIL=`$CMD $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=1;" | sed ':a;N;$!ba;s/\n/;/g'`
    
        # Here we get the "Home", "Mobile", Work" and "Other" (labeled) phone numbers.
        # NOTE: Each of these may return multiple responses, so we need to convert new-lines (\n) to ",".
        HPHONE=`$CMD $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=6 AND data2=1;" | sed ':a;N;$!ba;s/\n/,/g'`;
        MPHONE=`$CMD $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=6 AND data2=2;" | sed ':a;N;$!ba;s/\n/,/g'`;
        WPHONE=`$CMD $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=6 AND data2=3;" | sed ':a;N;$!ba;s/\n/,/g'`;
        OPHONE=`$CMD $CONTACTSDB "SELECT data1 FROM data WHERE raw_contact_id=$RAWID AND mimetype_id=6 AND data2=7;" | sed ':a;N;$!ba;s/\n/,/g'`;
        PHONES="$MPHONE,$HPHONE,$WPHONE,$OPHONE";

    Thanks so much for your post.
    1
    I can't thank you enough for this information!
    My S2 died on me a month back, and the only backup of my contacts (which are not synced on Gmail) were an old TB backup from April 2012. At least thanks to this knowledge I know what and where to look for!

    Thanks a lot ;)