在我的代碼中,我必須做一個類似條件的簡單sql查詢。 我這樣perl,dbi與類似條件的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();
但查詢已經做返回的所有行,忽略等等條件。 我的錯在哪裏?
感謝的
無關,但使用詞法文件句柄和3文件打開。例如打開(我的$,'<',$文件名)或死...; #3文件打開防止漏洞和詞彙限制範圍http://perldoc.perl.org/functions/open.html – xenoterracide 2010-07-21 14:32:21
哦,我也建議打破長行和更多空白。 – xenoterracide 2010-07-21 14:37:37