I am trying to write a ruby script that will parse a tab-delimited text file
and load the data into a database table. Here is my script:
require "dbi"
DBI.connect("DBI:OCI8:mydb", "myusername", "mypassowrd") do |dbh|
sth = dbh.prepare("INSERT INTO my_table VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")
f = File.open("data.txt", "r")
id = 0
f.each_line do |line|
values = [id]
values.concat line.chomp.split("\t")
(10..12).each {|x| values[x] = Time.parse(values[x]) unless
values[x].nil? || values[x].strip == ""}
sth.execute(values)
id += 1
end
end
Unfortunately, when it executes, I get this:
c:/ruby/ruby-1.8.4_16/lib/ruby/site_ruby/1.8/oci8.rb:664:in `bindByPos': Not
supported type (Array) (ArgumentError)
from c:/ruby/ruby-1.8.4_16/lib/ruby/site_ruby/1.8/oci8.rb:664:in`bind_or_define'
from c:/ruby/ruby-1.8.4_16/lib/ruby/site_ruby/1.8/oci8.rb:530:in`bind_param'
from c:/ruby/ruby-
1.8.4_16/lib/ruby/site_ruby/1.8/DBD/OCI8/OCI8.rb:146:in `bind_param'
from c:/ruby/ruby-1.8.4_16/lib/ruby/site_ruby/1.8/dbi/dbi.rb:1110:in`bind_params'
from c:/ruby/ruby-
1.8.4_16/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:21:in`each_with_index'
from c:/ruby/ruby-1.8.4_16/lib/ruby/site_ruby/1.8/dbi/dbi.rb:1110:in`bind_params'
from c:/ruby/ruby-1.8.4_16/lib/ruby/site_ruby/1.8/dbi/dbi.rb:776:in`execute'
from load_from_txt.rb:12
from load_from_txt.rb:8
from c:/ruby/ruby-1.8.4_16/lib/ruby/site_ruby/1.8/dbi/dbi.rb:590:in`connect'
from c:/ruby/ruby-1.8.4_16/lib/ruby/site_ruby/1.8/dbi/dbi.rb:384:in`connect'
from load_from_txt.rb:3
I think the problem is that the execute method expects a varargs, but it is
getting an Array. How do I convert the Array into varargs?