Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forward Slash issue with DBI

Tags:

perl

dbi

I'm new to using DBI for SQL queries in a perl script. The issue I'm having pertains to data in fields that have a forward slash. I'm wanting to use variables as input for my where clause, but it is doing what DBI intends a forward slash to do: stop the query. I tried numerous different work arounds from binds, quotes, etc. but none worked, is it even possible? Data in this is consistent. The line with the my $sql variable is where the trouble is.

#!/usr/bin/perl

# Modules
use DBI;
use DBD::Oracle;
use strict;
use warnings;

# Connection Info
$platform = "Oracle";
$database = "mydb";
$user = "user";
$pw = "pass";

# Data Source
$ds = "dbi:Oracle:$database";

my $dbh = DBI->connect($ds, $user, $pw);

# my $dbh = DBI->connect();       
my $XCOD = $dbh->quote('cba');
my $a = $dbh->quote('abc');
my $b = $dbh->quote('123');
# tried this as well  my $pid = $dbh->quote('$a/$b');
my $sql = "SELECT P_ID FROM MyTable WHERE P_ID=$a/$b AND XCOD=$XCOD";
my $sth = $dbh->prepare($sql);
$sth->execute(); 

my $outfile = 'superunique.txt';
open OUTFILE, '>', $outfile or die "Unable to open $outfile: $!";

while(my @re = $sth->fetchrow_array) {
print OUTFILE @re,"\n";
}

close OUTFILE;

$sth->finish();
$dbh->disconnect();
like image 941
Sheddy Avatar asked Feb 18 '26 14:02

Sheddy


1 Answers

I don't like to see folks use variable interpolation in SQL queries. Try using placeholders:

 [ snip ]
 my $P_ID = "$a/$b"
 my $sql = "SELECT P_ID FROM MyTable WHERE P_ID = ? AND XCOD = ?";
 my $sth = $dbh->prepare($sql);
 $sth->execute($P_ID, $XCOD); 
 [ snip ]
like image 53
Len Jaffe Avatar answered Feb 20 '26 09:02

Len Jaffe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!