dbi: passing parameters to execute

D

David Jacobs

Hi,

I'm fairly new to Ruby. I touched some Perl, looked at Python, but the
elegance and clarity of the code in the doc's/tutorials of Ruby
attracted me (I'm no expert-programmer at all so this is very
subjective!).

I can declare some parameters in a SQL-statement with ?-marks and I
can pass the appropriate value(s) with the method execute(). This is
easy when the sql-code is like
***
select *
from bla
where
year =3D ?
***

How do I pass values when my sql-code is something like
***
select *
from bla
where
year IN ?
***

Is this possible? Do I have to pass an array of integers? (I guess not
because it doesn't work...) I've been googling alot to find any help,
in vain -- I'm clueless.

Many thanks in advance!
Kind regards,
David
 
J

Jim Weirich

David Jacobs said:
How do I pass values when my sql-code is something like
***
select *
from bla
where
year IN ?
***

Is this possible? Do I have to pass an array of integers? (I guess not
because it doesn't work...) I've been googling alot to find any help,
in vain -- I'm clueless.

I don't think passing in an array will work (in all cases).

But you might try this (warning: untested code):

sql = "select * from bla where year in (" +
values.collect{'?'}.join(',') +
")";
db.select_all(sql, values) do |row|
# process row
end
 

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,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top