Question

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?

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top