Passing an array to DBI statement execute

sth.execute(*values)

You need to flatten the list here. Otherwise, you're just passing a
single array.

Regards,

Dan

This communication is the property of Qwest and may contain confidential or
privileged information. Unauthorized use of this communication is strictly
prohibited and may be unlawful. If you have received this communication
in error, please immediately notify the sender by reply e-mail and destroy
all copies of the communication and any attachments.

···

-----Original Message-----
From: pauljbarry3@gmail.com [mailto:pauljbarry3@gmail.com] On
Behalf Of Paul Barry
Sent: Tuesday, May 30, 2006 2:28 PM
To: ruby-talk ML
Subject: Passing an array to DBI statement execute

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 = Time.parse(values)
unless values.nil? || values.strip == ""}
    sth.execute(values)

that's it, thanks

···

On 5/30/06, Berger, Daniel <Daniel.Berger@qwest.com> wrote:

> -----Original Message-----
> From: pauljbarry3@gmail.com [mailto:pauljbarry3@gmail.com] On
> Behalf Of Paul Barry
> Sent: Tuesday, May 30, 2006 2:28 PM
> To: ruby-talk ML
> Subject: Passing an array to DBI statement execute
>
> 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 = Time.parse(values)
> unless values.nil? || values.strip == ""}
> sth.execute(values)

sth.execute(*values)

You need to flatten the list here. Otherwise, you're just passing a
single array.

Regards,

Dan

This communication is the property of Qwest and may contain confidential
or
privileged information. Unauthorized use of this communication is strictly
prohibited and may be unlawful. If you have received this communication
in error, please immediately notify the sender by reply e-mail and destroy
all copies of the communication and any attachments.

Ok, well, now it executes and seems like it is working, except nothing ends
up in my database. Any ideas? Is there any way to print out what sql
statement it is trying to execute? I can't find any API docs for DBI.

Also, is DBI smart enough to figure out how to convert the Time objects into
the correct format for the database?

···

On 5/30/06, Paul Barry <mail@paulbarry.com> wrote:

that's it, thanks

On 5/30/06, Berger, Daniel <Daniel.Berger@qwest.com> wrote:
>
> > -----Original Message-----
> > From: pauljbarry3@gmail.com [mailto:pauljbarry3@gmail.com] On
> > Behalf Of Paul Barry
> > Sent: Tuesday, May 30, 2006 2:28 PM
> > To: ruby-talk ML
> > Subject: Passing an array to DBI statement execute
> >
> > 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 = Time.parse(values)
> > unless values.nil? || values.strip == ""}
> > sth.execute(values)
>
> sth.execute(*values)
>
> You need to flatten the list here. Otherwise, you're just passing a
> single array.
>
> Regards,
>
> Dan
>
> This communication is the property of Qwest and may contain confidential
> or
> privileged information. Unauthorized use of this communication is
strictly
> prohibited and may be unlawful. If you have received this communication
> in error, please immediately notify the sender by reply e-mail and
destroy
> all copies of the communication and any attachments.
>