John Fremlin's blog: Reading your Facebook and other contacts from Android

Posted 2010-09-14 17:20:00 GMT

When I show people an android phone, they tend to call it a fake iPhone and then become impressed by some feature. Besides being cooler than a me-too device, the platform is relatively open to being fiddled with.

A case in point of this is the contacts database: it's stored at /data/ and you can read it with adb or with unyaffs from a nandroid backup. The database contains Facebook contacts and contacts attached to Google accounts (and the phone account if that's enabled). Facebook tries to stop you retrieving your data from it — this is one way to convert your Facebook friends to a vcard.

My phone was pick-pocketed in Hong Kong (it was quickly shipped out to a place called Po Lam and turned off). I have a nandroid backup of the contacts database and would like to feed it into a new phone -- the Facebook for Android is rather sticky about letting you "modify" a Facebook contact to add a number or something to it, so it would be rather fine to move Facebook contacts into normal contacts.

Using the sqlite3 program you can easily look at it. Among items of interest is the phone_lookup table which has a mapping of phone number to contact, with the phone number written backwards - like 144000949744+ for +447949000441. This is useful because caller id often comes up as 07949000441, with a different prefix and to look it up you probably do best by finding the longest match. There are additionally numerous useful "views" defined (queries that can to a certain extent be treated as tables).

Phone numbers and emails and things are stored in the data table (marked by mimetypes), and things are related by raw_contact_id: for example, to fetch phone numbers use

select raw_contact_id,data1 from data 
	    where mimetype_id=(SELECT _id FROM mimetypes 
	    	  where mimetype='')

and to relate names to id

SELECT _id,display_name FROM raw_contacts

I wished to preserve the following information for each contact: name, telephone numbers, email addresses, and physical addresses. Additionally, as my contacts have been collected from all sorts of different places, if any of the email addresses intersect, or the last eight digits of the phone number intersect, or the names intersect then the contacts should be merged intelligently (generally taking the longest of any field which matches sufficiently).

After a bit of fiddling, I came up with a grubby little Ruby program that takes a contacts2.db and prints out a vcard.

Thanks again Android!

Nice one John, and cheers!

I'd moved from a stock HTC build to a ROM and ContactsProvider was happily ignoring my previous contacts (which apparently were on my "HTC" account, presumably so they'd be synced with HTC's software).

Probably could have hacked about in the database myself, but used yer Ruby script to export the old database's contacts to a vCard and then reimported them.


Posted 2011-06-04 11:52:30 GMT by Anonymous from

Thanks a lot for this very useful script. I had been bone-headed and forgotten to backup contacts (on the cloud or otherwise) when I flashed GingerBread on my Desire, but did have the Nandroid backup of /data.

This handy script saved my day, thanks again!

Posted 2011-09-07 16:04:47 GMT by NMA

BIG THANK YOU!! I'm using a custom ROM not compatible with samsung Kies, I had a hope that somebody would have created a script to import from contacts2.db generating a VCF or something else, so I backed up my database hoping to import it soon and I got it!!

Posted 2011-09-16 15:26:28 GMT by Anonymous from

Hi thanks for the script but i am getting an error with executing the ruby program. Here is my log. can you tell me what is wrong?


C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require':

no such file to load -- sqlite3 (LoadError)

from C:/Ruby192/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:i

n `require'

from C:/Ruby192/c2v.rb:3:in `<main>'

Posted 2011-09-25 18:28:46 GMT by Anonymous from

I'm getting this error when trying to use your program

C:/Ruby192/lib/ruby/1.9.1/c2v.rb:43:in `+': can't convert NoMethodError into Str

ing (TypeError)

from C:/Ruby192/lib/ruby/1.9.1/c2v.rb:43:in `rescue in block (2 levels)

in vcard'

from C:/Ruby192/lib/ruby/1.9.1/c2v.rb:38:in `block (2 levels) in vcard'

from C:/Ruby192/lib/ruby/1.9.1/c2v.rb:37:in `each'

from C:/Ruby192/lib/ruby/1.9.1/c2v.rb:37:in `block in vcard'

from C:/Ruby192/lib/ruby/gems/1.9.1/gems/vpim-0.695/lib/vpim/vcard.rb:10

69:in `make'

from C:/Ruby192/lib/ruby/gems/1.9.1/gems/vpim-0.695/lib/vpim/vcard.rb:10

57:in `make2'

from C:/Ruby192/lib/ruby/1.9.1/c2v.rb:27:in `vcard'

from C:/Ruby192/lib/ruby/1.9.1/c2v.rb:185:in `merge'

from C:/Ruby192/lib/ruby/1.9.1/c2v.rb:191:in `block in <main>'

from C:/Ruby192/lib/ruby/1.9.1/c2v.rb:190:in `each'

from C:/Ruby192/lib/ruby/1.9.1/c2v.rb:190:in `<main>'

any help is appreciated

Posted 2011-11-03 16:09:22 GMT by Raul from

How do I use the ruby script?! Do I do:

ruby <input_db_file> contacts2vcard.rb

do I have to specify an output file? Do I need SQLite3 installed on my windows box for this to all work, or is it all done within your script?

my email is mroslevich[AT]gmail[DOT]com. please let me know, as this script is definitely what I was looking for!

Posted 2011-12-21 16:47:18 GMT by Anonymous from

@Anonymous (

execute these lines before trying to run the script

gem install sqlite3

gem install vpim


i deleted "+ $!" from line 43 and the script ran fine

@Anonymous (

do those lines i told the other anonymous to do to get sqlite3 then just run the script in the same folder as contacts2.db

Posted 2012-01-11 18:16:36 GMT by Chris

actually just noticed it left errors all over the place so i changed line 43 to

$stderr.print ""

also i dont think it actually outputs a file, you'll need to increase the buffer size of your command prompt so you can mark and copy all the output that it leaves in there and paste it into a text file and rename the file filename.vcf

Posted 2012-01-11 19:00:50 GMT by Chris

I'm so sorry for spamming posts, just remembered you can do

contacts2-to-vcard.rb > file.vcf

to get it to put it straight into a vcf file

Posted 2012-01-11 19:05:07 GMT by Chris

I was trying to run this script today to recover my contacts2.db file as vcard and got the following error from ruby. The sqlite3 and vpim installs were successful. Any suggestions what could be wrong?

D:/Utilities/Ruby192/lib/ruby/1.9.1/set.rb:85:in `do_with_enum': value must be e

numerable (ArgumentError)

from D:/Utilities/Ruby192/lib/ruby/1.9.1/set.rb:310:in `merge'

from D:/Utilities/Ruby192/lib/ruby/1.9.1/set.rb:75:in `initialize'

from D:/Temp/contacts2-to-vcard.rb:140:in `new'

from D:/Temp/contacts2-to-vcard.rb:140:in `block in <main>'

from D:/Utilities/Ruby192/lib/ruby/1.9.1/set.rb:222:in `block in each'

from D:/Utilities/Ruby192/lib/ruby/1.9.1/set.rb:222:in `each_key'

from D:/Utilities/Ruby192/lib/ruby/1.9.1/set.rb:222:in `each'

from D:/Temp/contacts2-to-vcard.rb:139:in `<main>'

Posted 2012-07-22 06:48:04 GMT by Mango

Trying to run on OSX 10.6.8 and getting the following error.

contacts2-to-vcard.rb:1:in `require': no such file to load -- vpim/vcard (LoadError)

How do I fix this? If I can get this to work you will be my hero.

Posted 2012-08-26 19:29:12 GMT by lunarlandr from

Sorry for my english, i'm from argentina

when i do "contacts2-to-vcard.rb > contactacts.vcf" i see a "windows cannot opend this file" message "file:contacts2-to-vcard.rb"

Posted 2012-08-28 11:25:54 GMT by Anonymous from

I write my own version of script on perl. Can be use for russian contacts.

Сaution! Beta! Ugly code =)

Posted 2012-12-15 05:42:34 GMT by Anonymous from

could u please help me to convert my contacts2.db to vcf??

Many thanks

Posted 2014-02-09 05:16:52 GMT by Anonymous from

i already try and the out put already on vcf file but the problem size of this .vcf file 0...somebody can help my problem????

Many thanks

Posted 2014-02-09 07:04:35 GMT by Anonymous from


Are you still in business?

I found a few errors on your site.

Would you like me to send over a screenshot of those errors?



(714) 500-7363

Posted 2022-10-07 17:16:12 GMT by Jacob Lewis


Wondering if you accept link inserts on existing posts on

How much would you charge for this?

Also, do you have any other sites for link inserts?


If you'd like to unsubscribe click the following link.

Posted 2022-11-05 14:51:23 GMT by Justin McCarty


Are you still in business?

I found a few errors on your site.

Would you like me to send over a screenshot of those errors?



(714) 500-7363

Posted 2023-02-05 16:03:26 GMT by Jacob Lewis


Are you still in business?

I found a few errors on your site.

Would you like me to send over a screenshot of those errors?



(714) 908-9255

Posted 2023-03-16 11:20:26 GMT by Joe Celine

Post a comment