# Passing DBI parameters as an array

**URL:** <https://www.ruby-forum.com/t/passing-dbi-parameters-as-an-array/105232>\
**Category:** Ruby\
**Created:** [May 27, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/passing-dbi-parameters-as-an-array/105232 "2007-05-27T19:57:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 27, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/passing-dbi-parameters-as-an-array/105232/1 "2007-05-27T19:57:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [May 27, 2007, 8:04pm UTC](https://www.ruby-forum.com/t/passing-dbi-parameters-as-an-array/105232/2 "2007-05-27T20:04:06Z")

</div>

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!
