views:

64

answers:

1

hi all, 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

+4  A: 

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";
M42
ok, i'll try now. i've try to concat the '%' but in my $query.
zebra
You cannot use the `%` char in your query, as you're using the `?` placeholder which automatically quotes your `$valore` variable (which is a good thing to do). That means that using `%` in your *query* leads to a string which looks like `"somevalue"%` while it needed to look like `"somevalue%"`.
Jonas