Passing DBI parameters as an array

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

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!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs