我正在尝试使用一组值的占位符实现 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";
}
当我尝试进行更改以便为 array 中存在的一组值实现上述代码时@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";
}
关于出了什么问题以及如何纠正它的任何建议?谢谢!