Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

perl DBI and placeholders

Tags:

perl

dbi

I have this query select * from table where ID in (1,2,3,5...)

How is it possible to build this query with the DBI using placeholders ?

for example :

my @list = (1, 2, 3, 4, 5);
my $sql = "select * from table where ID in (?)";

$sth->prepare($sql);
$sth->execute();

What argument should I send to execute? Is it a list or a string separated by , or something else?

like image 284
smith Avatar asked Oct 22 '11 18:10

smith


1 Answers

If you are using DBI to access a PostgreSQL database with the DBD::Pg driver, you can use:

my @list = (1, 2, 3, 4, 5);
my $sql = "select * from table where ID = ANY(?::INT[]);";

$sth->prepare ($sql);
$sth->execute (\@list);
like image 68
Tim Landscheidt Avatar answered Sep 24 '22 02:09

Tim Landscheidt