1

我正在尝试使用一组值的占位符实现 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";
}

关于出了什么问题以及如何纠正它的任何建议?谢谢!

4

1 回答 1

4

您没有从从文件中读取的行中删除 EOL 标记。这是修复它:

my @l_names = <FH>; 
chomp(@l_names);

更新:

您还可以查看“如何使用 Perl 的 DBI 模块中的数组中的参数选择多行?” 问题

于 2009-12-07T17:20:39.603 回答