Well, when I had to do this recently, I used FasterCSV and ActiveRecord inside a Rails migration which began like:
require 'rubygems' require 'active_record/fixtures' begin gem 'fastercsv', '>=1.2' rescue Exception # on TxD, it's unpacked as a plugin! $: << File.expand_path('vendor/plugins/fastercsv-1.2.0/lib') end require 'fastercsv'
class ImportResearchData < ActiveRecord::Migration ...
And had a part that was like:
FasterCSV.open(research_data, :headers => :first_row, :converters => :numeric) do |csv| csv.each do |row| if csv.lineno % 1000 == 1 $stderr.print "\r%5d ..." % csv.lineno $stderr.flush end row.each do |header,value|
...and used the block variables to create one of three different records, if needed, through ActiveRecord models. You should be able to figure out how to get started. Note that if your CSV file is one-record equals one-database-row, your loop will likely be quite a bit simpler than mine. (My CSV data was essentially the result that would come from a three table join of the original database to which I had no access.)
-Rob
Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com