CSV file to database problem - FasterCSV::MalformedCSVError

J

John Mcleod

Hello all,

I'm having problems importing a simple csv into the sqlite database.

When I execute "db:migrate --trace", I get
"FasterCSV::MalformedCSVError". I've check the .csv file and made sure
that it's UTF-8 encoded.

Here's my migration file:

require 'faster_csv'
class AddTestData < ActiveRecord::Migration

def self.up
Project.delete_all
array_of_arrays = FasterCSV.foreach('filename.csv') do |row|

record = Project.new(
:irb_number => row[0],
:pi_full_name => row[1],
:cr_quest_split => row[2],
:cr_and_ct_split => row[3],
:eek:ld_master_list => row[4],
:title => row[5],
:status_of_irb => row[6],
:expiration_date => row[7],
:review_level => row[8],
:category => row[9],
:q2_study_coordinator_last_name => row[10],
:q2_study_coordinator_first_name => row[11]
)
record.save

end
end

"Projects" is the name of the table and all of the symbols are columns
within the table.

Any help or a pointer to a good example is appreciated.

Thanks,
JohnM
 
J

John Mcleod

Solved my own problem.

Found an import script at
http://espndev.com/blog/csv-imports-using-fastercsv/

Here it is:

require 'fastercsv'

class LoadRatingsData < ActiveRecord::Migration
def self.up
FasterCSV.foreach("#{RAILS_ROOT}/db/migrate/fixtures/ratings.csv",
:row_sep => "\r") do |row|
id,game_id, region_id, rating, duration = row
Rating.create:)id => id, :game_id => game_id, :region_id => region_id,
:rating => rating, :duration => duration)
end
end

def self.down
end
end

I updated all table/column info. for my database info.

Regards,

JohnM


John said:
Hello all,

I'm having problems importing a simple csv into the sqlite database.

When I execute "db:migrate --trace", I get
"FasterCSV::MalformedCSVError". I've check the .csv file and made sure
that it's UTF-8 encoded.

Here's my migration file:

require 'faster_csv'
class AddTestData < ActiveRecord::Migration

def self.up
Project.delete_all
array_of_arrays = FasterCSV.foreach('filename.csv') do |row|

record = Project.new(
:irb_number => row[0],
:pi_full_name => row[1],
:cr_quest_split => row[2],
:cr_and_ct_split => row[3],
:eek:ld_master_list => row[4],
:title => row[5],
:status_of_irb => row[6],
:expiration_date => row[7],
:review_level => row[8],
:category => row[9],
:q2_study_coordinator_last_name => row[10],
:q2_study_coordinator_first_name => row[11]
)
record.save

end
end

"Projects" is the name of the table and all of the symbols are columns
within the table.

Any help or a pointer to a good example is appreciated.

Thanks,
JohnM
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top