Perl, dbi with sql statement with similar condition

in my code I have to make a simple SQL query with a similar condition. i do so

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 returns all rows, ignoring a similar condition. Where is my fault?

Thanks x

+3
source share
1 answer

You need to map% char to the variable you are looking for.

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

Source: https://habr.com/ru/post/1755761/


All Articles