Forum: Ruby on Rails How I Import file CSV into database

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
35b48f6b12d9f338ec94ea01233a776b?d=identicon&s=25 watxit (Guest)
on 2007-03-02 03:36
Hi there.
I don't know how to Import CSV file into my database.Can you help me
code it.thks
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2007-03-02 05:07
(Received via mailing list)
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'
   gem 'fastercsv', '>=1.2'
rescue Exception
   # on TxD, it's unpacked as a plugin!
   $: << File.expand_path('vendor/plugins/fastercsv-1.2.0/lib')
require 'fastercsv'

class ImportResearchData < ActiveRecord::Migration

And had a part that was like:,
                      :headers           => :first_row,
                      :converters        => :numeric) do |csv|
         csv.each do |row|
           if csv.lineno % 1000 == 1
             $stderr.print "\r%5d ..." % csv.lineno
           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 Biedenharn
C1607873b99845b2cd53f8634860d4d4?d=identicon&s=25 Benoit B. (Guest)
on 2007-03-02 09:36
(Received via mailing list)
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.
72ee7b478a235f646c3495f03ebbe676?d=identicon&s=25 Andrew Stewart (Guest)
on 2007-03-02 10:27
(Received via mailing list)

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:

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.

Andy Stewart
35b48f6b12d9f338ec94ea01233a776b?d=identicon&s=25 watxit (Guest)
on 2007-03-06 03:58
Thanks all.
here are my code:

def upload
     CSV::Reader.parse('file.csv'))  do |row|
      row_count += 1
      if row_count != 1
      this_row = {:name => row[0],
                  :email=> row[1]

      @contacts =!

   flash[:notice] = "Imported #{row_count} rows"
It good! :)
This topic is locked and can not be replied to.