开发者

Using Perl bind_param with SQL IN statement [duplicate]

开发者 https://www.devze.com 2023-01-18 01:30 出处:网络
This question already has answers here: 开发者_StackOverflow Closed 12 years ago. Possible Duplicate:
This question already has answers here: 开发者_StackOverflow Closed 12 years ago.

Possible Duplicate:

Is there SQL parameter binding for arrays?

I was wondering if there is anyway to use bind_param with SQL IN statements. According to perl documentation bind_param_array cannot be used as well. Has anyone come across the same situation?

http://search.cpan.org/perldoc?DBI#bind_param_array


No you can't do this easily. One option is to use $dbh->quote, eg.

my @values = (1,2,3,4);
my $sql = "SELECT * from my_table WHERE ID IN (";
$sql .= join(',', map { $dbh->quote($_) } @values)
$sql .= ')';

Or you can create the necessary placeholders and pass the array in as bind parameters, eg.

my @values = (1,2,3,4);
my $sql = "SELECT * from my_table WHERE ID IN (";
$sql .= join(',', map { '?' } @values);
$sql .= ')';

my $sth = $dbh->prepare($sql);
$sth->execute(@values);

Neither is exceptionally pretty.


Not if you're wanting an arbitrary number of placeholders, no. You can use it with an IN, as in

where foo in ( ?, ?, ? )

but then you must have exactly three binds.

0

精彩评论

暂无评论...
验证码 换一张
取 消