perl,dbi与sql语句具有相似的条件

在我的代码中,我必须做一个类似条件的简单SQL查询.
我是这样做的

my $out = "/Users/zero/out.log";
my $filename = "/Users/zero/data.txt";


my $dbh = DBI->connect("DBI:Oracle:sid=$sid;host=$host;port=$port", $user, $pwd) or die "Couldn't connect to database: " . DBI->errstr;
my $query = "select SOMETHING from SOMETHING_1 where SOMETHING like ?||'%' ";
my $sth = $dbh->prepare($query) or die "Connection Error: " . $dbh->errstr;
open (IN,"< $filename") or die("Unable to open $filename");        
my @righe = <IN>;
close IN;
open (OUT,">$out") or die "Unable to open $out";
foreach my $riga (@righe) {
        chomp $riga;
        (my $valore) = split (/\n/, $riga);
        $sth->execute($valore) ||print "Impossibile eseguire la query $query";
        while (my $real = $sth->fetchrow_array) {
                       print OUT "\"$real\"\n";
                    }
    }
$sth->finish;
$dbh->disconnect();

但查询返回所有行,忽略了类似的条件.
哪里是我的错?

谢谢

最佳答案
您必须将%char连接到您搜索的变量.

my $query = "select SOMETHING from SOMETHING_1 where SOMETHING like ?";
...
$sth->execute($valore.'%') ||print "Impossibile eseguire la query $query";

转载注明原文:perl,dbi与sql语句具有相似的条件 - 代码日志