# CSV file to database problem - FasterCSV::MalformedCSVError

**URL:** <https://www.ruby-forum.com/t/csv-file-to-database-problem-fastercsv-malformedcsverror/172200>\
**Category:** Ruby\
**Created:** [July 16, 2009, 5:06pm UTC](https://www.ruby-forum.com/t/csv-file-to-database-problem-fastercsv-malformedcsverror/172200 "2009-07-16T17:06:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [July 16, 2009, 5:06pm UTC](https://www.ruby-forum.com/t/csv-file-to-database-problem-fastercsv-malformedcsverror/172200/1 "2009-07-16T17:06:07Z")

</div>

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],
                     :old_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

---

<div class="post-metadata">

**Author:** ![jmaenpaa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de053/32.png) [@jmaenpaa](https://www.ruby-forum.com/u/jmaenpaa)\
**Post date:** [July 16, 2009, 5:37pm UTC](https://www.ruby-forum.com/t/csv-file-to-database-problem-fastercsv-malformedcsverror/172200/2 "2009-07-16T17:37:26Z")

</div>

Solved my own problem.

Found an import script at  
[http://espndev.com/blog/csv-imports-using-fastercsv/](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 M. wrote:

> 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],
> :old_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
