Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I import a CSV file into an SQLite database with the sqlite3 gem for Ruby

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?

like image 618
Username Avatar asked Dec 15 '22 20:12

Username


2 Answers

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]]
like image 87
Joshua Cheek Avatar answered May 16 '23 00:05

Joshua Cheek


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.

like image 40
jefflunt Avatar answered May 16 '23 02:05

jefflunt