问题
in my code i must do a simple sql query with a like condition. i've do in this way
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();
but the query return all the rows, ignoring the like condition. Where's my fault?
Thank's
回答1:
You have to concat the % char to the variable you search for.
my $query = "select SOMETHING from SOMETHING_1 where SOMETHING like ?";
...
$sth->execute($valore.'%') ||print "Impossibile eseguire la query $query";
来源:https://stackoverflow.com/questions/3300012/perl-dbi-with-sql-statement-with-a-like-condition