[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Passing DBI parameters as an array

Sebastian probst Eide

5/27/2007 5:58:00 PM

Hi
I am dynamically creating an insert statement for a database that I want
DBI to execute.

In the first part of my example below I am passing each value I want
escaped to the database handlers "do" method as a separate argument.
That works fine. What I'd need is a way to pass the values I want
inserted as an array as the number of inserts are going to change each
time the script is run. Is there a way to do that?
I could escape the characters myself and insert them directly into the
sql statement, but I thought I would check with you guys first if you
know of another and better way to do this.

DBI.connect("DBI:Mysql:#{DB_DATABASE}:#{DB_SERVER}", DB_USER,
DB_PASSWORD) do |dbh|
value1 = "Something"
value2 = "Something"
value3 = "Something"
array_in = [value1, value2, value3]
sql_insert = "INSERT INTO users (email) VALUES (?), (?), (?)"

#This works...
dbh.do(sql_insert, value1, value2, value3)

#This is what I need to work
dbh.do(sql_insert, array_in)
end

Best regards
Sebastian

--
Posted via http://www.ruby-....

1 Answer

Sebastian probst Eide

5/27/2007 6:04:00 PM

0

Sorry... I could have though a little bit more myself too before posting
to the forum.
I just have to expand the array:

DBI.connect("DBI:Mysql:#{DB_DATABASE}:#{DB_SERVER}", DB_USER,
DB_PASSWORD) do |dbh|
value1 = "Something"
value2 = "Something"
value3 = "Something"
array_in = [value1, value2, value3]
sql_insert = "INSERT INTO users (email) VALUES (?), (?), (?)"

#This is what I need to work
dbh.do(sql_insert, *array_in)
end

and then it works!

never mind!

--
Posted via http://www.ruby-....