How do I export data from CSV to DataMapper in Sinatra?

474 views Asked by At

I'm building a simple web application using Sinatra.

I have an external text file and would like to parse it using CSV. Then I would like to export the data and create a database using DataMapper. I'm stuck on how to accomplish this.

This is what I have so far:

require 'sinatra'
require 'csv'
require 'data_mapper'

CSV.foreach("words.txt") do |row|
  puts row[0]
end

DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/scrabble.db")


class Letters  
  include DataMapper::Resource  
  property :id, Serial  
  property :content, Text, :required => true    
  property :created_at, DateTime  
  property :updated_at, DateTime  
end 

Can anyone point me to the right direction?

1

There are 1 answers

0
Ivan Shamatov On BEST ANSWER

if you itterate through a list of words, you need to do like that:

require 'sinatra'
require 'csv'
require 'data_mapper'


DataMapper::setup(:default, "sqlite3://#{Dir.pwd}/scrabble.db")


class Letters  
  include DataMapper::Resource  
  property :id, Serial  
  property :content, Text, :required => true    
  property :created_at, DateTime  
  property :updated_at, DateTime  
end 


CSV.foreach("words.txt") do |row|
  Letters.create(content: row[0])
end