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?
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]]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With