Passing DBI parameters as an array

  • Thread starter Sebastian probst Eide
  • Start date
S

Sebastian probst Eide

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
 
S

Sebastian probst Eide

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top