我正在尝试使用占位符为一组值实现sql LIKE限定符。我知道如何为一个特定的值做如下:
use strict;
use DBI;
my $dbh = DBI->connect("dbi:Sybase:server=$Srv;database=$Db", "$user", "$passwd") or die "could not connect to database";
my $query = "Select key, first_name from names where last_name like ? ";
my $sth = $dbh->prepare( $query ) or die "could not prepare statement\n", $dbh->errstr;
$sth->bind_param(1, "Mar%") or die "Could not bind_param", $sth->errstr ;
$sth->execute or die "Could not execute $query", $sth->errstr;
while (my @result = $sth->fetchrow_array){
print "@result\n";
}
当我尝试进行更改以便为数组@l_names
中存在的一组值实现上述代码时,我没有得到任何结果。我这样做:
use strict;
use DBI;
open (FH, "/home/usr/file") or die $!;
my @l_names= <FH>;
close(FH);
my $dbh = DBI->connect("dbi:Sybase:server=$Srv;database=$Db", "$user", "$passwd") or die "could not connect to database";
my $query = "Select key, first_name from names where last_name like ? ";
my $sth = $dbh->prepare( $query ) or die "could not prepare statement\n", $dbh->errstr;
foreach my $l_name (@l_names){
$sth->bind_param(1, "$l_name%") or die "Could not bind_param", $sth->errstr ;
$sth->execute or die "Could not execute $query", $sth->errstr;
}
while (my @result = $sth->fetchrow_array){
print "@result\n";
}
有关出了什么问题以及如何纠正错误的建议? 谢谢!
答案 0 :(得分:4)
您没有从已从文件中读取的行中删除EOL标记。这是修复它:
my @l_names = <FH>;
chomp(@l_names);
<强>更新强>
您还可以查看"How can I select multiple rows using parameters from an array in Perl’s DBI module?"问题