I have the following Ruby script: It creates a database, reads a csv file and inserts each row into the database.
require "sqlite3"
require "csv"
require "pp"
begin
db = SQLite::Database.new("myDB.db")
db.execute("CREATE TABLE IF NOT EXISTS MYTABLE(Id INTEGER PRIMARY KEY AUTOINCREMENT,
stations TEXT, dayparts TEXT, age TEXT, rtg DOUBLE, reach DOUBLE")
myData = {}
CSV.foreach("test_file.csv", :headers=>true, :header_converters => :symbol, :converters => :all)
do |row|
row.to_hash.each do |key, value|
mydata[key.to_sym] = value
end
db.execute("INSERT INTO myDB(stations, dayparts, age, rtg, reach) VALUES(?,?,?,?,?)",
myDATA[:stations], myData[:dayparts], myData[:age], myData[:rtg], mydata[:dlyrch000])
end
rescue SQLite3::Exception => e
puts "Exception occured"
puts e
ensure
db.close if db
end
When I run this script with static data. that is this line:
db.execute("INSERT INTO myDB(stations, dayparts, age, rtg, reach) VALUES(?,?,?,?,?)",
myDATA[:stations], myData[:dayparts], myData[:age], myData[:rtg], mydata[:dlyrch000])
is replaced by this:
db.execute("INSERT INTO myDB(stations, dayparts, age, rtg, reach) VALUES(?,?,?,?,?)",
test, test, 33, .8989, .23434)
A database is created with this data.
But when I try this script as above it throws an exception:
syntax error, unexpected ",", expecting ')' ... rtg, reach) VALUES (?,?,?,?,?)",
---> myData [:stations], myData[....
etc.
I have tried different options but cannot seem to get around this. Can someone please help me with this