Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Script to load a CSV phonebook file into the Presence Phonebook
- require 'rubygems'
- require 'fastercsv'
- require 'active_record'
- require 'composite_primary_keys'
- # connect to the database (Oracle in this case)
- ActiveRecord::Base.establish_connection({
- :adapter => "oracle",
- :username => "uname",
- :password => "passwd",
- :database => "10.0.1.198/xe"
- })
- #Create our class for the PCO_PHONEBOOK table
- class PcoPhonebook < ActiveRecord::Base
- set_table_name "PCO_PHONEBOOK"
- set_primary_keys :serviceid, :phone
- end
- puts "Starting Phonebook loader..."
- puts " "
- #Create our counters to check status as we go
- cnt = 0
- increment_cnt = 0
- #Open the CSV file and loop through each record
- FasterCSV.foreach("NationsHealth_Phonebook.csv") do |row|
- #Lookup the current row to see if it already exists in the database
- phonebook_entry = PcoPhonebook.find(:first, :conditions => [ "serviceid = ? AND phone = ?", row[0], row[1] ])
- #If the record exists, simply update the description field
- if phonebook_entry != nil
- phonebook_entry.description = row[3]
- phonebook_entry.save
- #If the record does not exist, then create a new entry
- else
- new_phonebook_entry = PcoPhonebook.new
- new_phonebook_entry.serviceid = row[0]
- new_phonebook_entry.phone = row[1]
- new_phonebook_entry.phonetype = row[2]
- new_phonebook_entry.description = row[3]
- new_phonebook_entry.save
- end
- cnt += 1
- increment_cnt += 1
- if increment_cnt == 100
- puts 'Records processed: ' + cnt.to_s
- increment_cnt = 0
- end
- end
- puts " "
- puts "Total records processed == " + cnt.to_s
Add Comment
Please, Sign In to add comment