Converting .db to .vcf

15,383

Solution 1

Yank it out of the DB (this is all in Ruby):

require 'sqlite3'

path_to_contactsdb_file = "/SAMPLE/PATH/TO/contacts2.db"

db = SQLite3::Database.new path_to_contactsdb_file

raw_contacts = db.execute("select _id,display_name from raw_contacts")
contacts = {}
raw_contacts.each do |x|
   contacts[x[1]] = {}
   contacts[x[1]]['rcid'] = x[0]
   contacts[x[1]]['nums'] = db.execute("select normalized_number from phone_lookup where raw_contact_id=" + x[0].to_s)
end

Pull it to CSV:

output_filepath = "/SAMPLE/EXAMPLE/FILEPATH"    

csv = ""
contacts.each do |k,v|
 csv += '"' + k + '",'
  v['nums'].each do |num|
    csv += '"' + num[0] + '",'
  end
  csv += "\n"
end

File.open(output_filepath,"w") {|file| file.write(csv) }

Then you can use a CSV import app, do it through Google contacts import through CSV, etc.. You can send me a fruit basket.

If it MUST be in a VCF format, then just change the output syntax of the CSV. Look up a sample VCF file, I couldn't be bothered.

Solution 2

I've been trying to use Dustin van Schouwen's code to do this task. I wanted to thank him for it, first of all, and then mention a few details I needed to add/change to make it work for me:

This line:

db = SQLite3::Database.new path_to_contactsdb_file

I changed to:

db = SQLite3::Database.new ("contacts2.db")

As when using the previous one, even though it doesn't give an error, it doesn't seem to actually connect to the database (I think it creates an empty one).

The other change I needed to do was at this other line:

csv += '"' + k + '",

If "k" is nil, the code will fail, so I introduced an if (ternary syntax) to solve it, and it worked fine:

csv += '"' + (k == nil ? "" : k) + '",'
Share:
15,383
MuntingInsekto
Author by

MuntingInsekto

A man is but the product of his thoughts. What he thinks, he becomes.

Updated on June 26, 2022

Comments

  • MuntingInsekto
    MuntingInsekto almost 2 years

    I accidentally deleted my contacts and i don't have a backup for that now i got the contacts2.db and i am trying to covert the .db to .vcf. Now i am using Ruby to convert the file and here is what i did

    gem install sqlite3
    gem install vpim
    path to contacts2-to-vcard.rb/contacts2-to-vcard.rb > contacts.vcf
    

    i always says 'Access Denied.' And i set the folder to full control but whenever i run that command it change to read only, by the way i am using windows 8. Any help? Or is there a alternate why to convert .db to .vcf? TIA