Question

Here's an example of my .CSV file:

column1,column2
data,moreData
foo,bar
cats,dogs

Using Ruby and the sqlite3 gem, is there a way to make an SQLite database and import the .CSV data into it?

Was it helpful?

Solution

Parse CSV with the csv lib, then just insert it like normal.

require "sqlite3"
require 'csv'

db = SQLite3::Database.new ":memory:"

# Create a database
rows = db.execute <<-SQL
  create table users (
    name varchar(30),
    age int
  );
SQL

csv = <<CSV
name,age
ben,12
sally,39
CSV

CSV.parse(csv, headers: true) do |row|
  db.execute "insert into users values ( ?, ? )", row.fields # equivalent to: [row['name'], row['age']]
end

db.execute( "select * from users" ) # => [["ben", 12], ["sally", 39]]

OTHER TIPS

I don't believe import/export of data via CSV is something that is done via the sqlite3 gem itself. You get data into/out of Ruby objects via a CSV library, and then it's just a matter of reading/writing the data to the DB via ActiveRecord, or whatever ORM it is that you're using.

FasterCSV should do it for you in Ruby. If you happen to also be using Rails 3.2, FasterCSV is, I believe, the default implementation behind the scenes of Rails' CSV lib.

You can call sqlite3 binary from ruby:

%x(
sqlite3 "#{pathToDatabase}" << EOF
.mode csv
.separator ',' "\\\\n"
.import "#{pathToCsvFile}" "#{csvTableName}"
EOF
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top