mardi 13 octobre 2015

How do I create a .csv file with specific sqlite table fields using ruby and sequel gem?

The bottom code is supposed to create a new test table, add data from two existing tables and add the contents to a new csv file. The parsing of both existing tables works fine, and the .csv also gets created without error. The issue I'm having is that the .csv is being created with blank data except for the 3 "Fixnum" header columns above. I've tried multiple different things including numerous variations of string interpolation.

require 'sequel'

# Create test case
DB.create_table(:"#{table}"){
Fixnum :"News-1"
Fixnum :"News-2"
Fixnum :"Item-1"
}

# Parse news table
DB['SELECT one, two FROM news WHERE Id=12;'].each do |news|
#p news
end

# Parse item table
DB['SELECT news_feed FROM item;'].each do |item|
#p item
end

# Insert data into csv_file
1.times{|i|
DB[:"#{table}"].insert("#{news}", "#{news}", "#{item}")
}

# Create .csv file
File.new("#{csv_file}", 'w')

# Open .csv file, write and save
File.open("#{csv_file}", 'w'){|f|
f << DB[:"#{table}"].to_csv                        
}

Aucun commentaire:

Enregistrer un commentaire