perl, dbi with sql statement with a like condition

梦想的初衷 提交于 2019-12-12 13:15:27

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!