# How I Import file CSV into database

**URL:** <https://www.ruby-forum.com/t/how-i-import-file-csv-into-database/87378>\
**Category:** Rails\
**Created:** [March 2, 2007, 3:36am UTC](https://www.ruby-forum.com/t/how-i-import-file-csv-into-database/87378 "2007-03-02T03:36:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![watxit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/85e7bf/32.png) [@watxit](https://www.ruby-forum.com/u/watxit)\
**Post date:** [March 2, 2007, 3:36am UTC](https://www.ruby-forum.com/t/how-i-import-file-csv-into-database/87378/1 "2007-03-02T03:36:57Z")

</div>

Hi there.  
I don’t know how to Import CSV file into my database.Can you help me  
code it.thks

---

<div class="post-metadata">

**Author:** ![watxit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/85e7bf/32.png) [@watxit](https://www.ruby-forum.com/u/watxit)\
**Post date:** [March 2, 2007, 5:07am UTC](https://www.ruby-forum.com/t/how-i-import-file-csv-into-database/87378/2 "2007-03-02T05:07:30Z")

</div>

On Mar 1, 2007, at 9:36 PM, watxit wrote:

> Hi there.  
> I don’t know how to Import CSV file into my database.Can you help me  
> code it.thks

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 B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![watxit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/85e7bf/32.png) [@watxit](https://www.ruby-forum.com/u/watxit)\
**Post date:** [March 2, 2007, 9:36am UTC](https://www.ruby-forum.com/t/how-i-import-file-csv-into-database/87378/3 "2007-03-02T09:36:49Z")

</div>

For occasional use, (or not, now that I think of it), you can also use  
MySQL to import it. A gui like Navicat can be a great time server in  
that case.  
For other DB, please check the doc.

---

<div class="post-metadata">

**Author:** ![watxit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/85e7bf/32.png) [@watxit](https://www.ruby-forum.com/u/watxit)\
**Post date:** [March 2, 2007, 10:27am UTC](https://www.ruby-forum.com/t/how-i-import-file-csv-into-database/87378/4 "2007-03-02T10:27:56Z")

</div>

Hello,

On 2 Mar 2007, at 02:36, watxit wrote:

> I don’t know how to Import CSV file into my database.Can you help me  
> code it.thks

This article on Migrating Data Into Rails might be of interest to you:

[http://blog.airbladesoftware.com/2006/12/5/migrating-data-into-rails](http://blog.airbladesoftware.com/2006/12/5/migrating-data-into-rails)

If you want to massage the values in your CSV file, you’ll probably  
be better off using FasterCSV to read the CSV file, general Ruby to  
massage the data as desired, and finally Active Record to save the  
values in the database.

On the other hand, if you just want to lever the values straight into  
the database without alteration, it’ll be quicker and simpler to use  
your database’s batch import facility. There’s a link to the MySQL  
one in the article above.

Regards,  
Andy S.

---

<div class="post-metadata">

**Author:** ![watxit](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/85e7bf/32.png) [@watxit](https://www.ruby-forum.com/u/watxit)\
**Post date:** [March 6, 2007, 3:58am UTC](https://www.ruby-forum.com/t/how-i-import-file-csv-into-database/87378/5 "2007-03-06T03:58:58Z")

</div>

Thanks all.  
here are my code:

def upload  
row\_count=0  
CSV::Reader.parse(File.open(‘file.csv’)) do |row|  
row\_count += 1  
if row\_count != 1  
this\_row = {:name =\> row[0],  
:email=\> row[1]  
…

```
           }
  @contacts = Contact.new(this_row)
  @contacts.user_id=current_user.id
  @contacts.save!

end

```

end  
flash[:notice] = “Imported #{row\_count} rows”  
end  
It good! 🙂
