I've a Perl script that builds up an sql cmd to set certain fields to NULL in a certain table in an MS Access db ( sorry). Here's a simplified mockup.
my $nonKeyFields_hashref = { "country" => "ZZZ",
"address3" => "FOO"
};
my $keyFields_hashref = { "address1" => "1212 O'Mally Street", # embedded single quote here is causing the problem
"client ID" => "1234567"
};
my $sqlCmd = "UPDATE myTable SET ";
$sqlCmd .= join( ", " , map{ "[?} = NULL "} keys $nonKeyFields_hashref;
$sqlCmd .= " WHERE ";
$sqlCmd .= join( " AND " , map{ "[?} = ? "} keys $keyFields_hashref;
# sqlCmd contains "UPDATE myTable SET [?] = NULL, [?} = NULL WHERE [?] = ? AND [?] = ?"
$sth = $dbh->prepare( $sqlCmd);
if( !defined( $sth)) {
_pushErrorMsg("sth failed to define - ".$DBI::errstr);
$errorHit = 1;
} else {
my @cmd_arry = ();
push( @cmd_arry, $_ ) for keys $nonKeyFields_hashref;
push( @cmd_arry, $_ , $keyFields_hashref->{$_} ) for keys $keyFields_hashref;
print Dumper( @cmd_arry);
# dumper shows @cmd_arry contains ("country", "address3", "address1", "1212 O'Mally Street", "client ID", "1234567")
# which is six elements, which jibes with the query's question-marks
$sth->execute( @cmd_arry); # errors here with the given message
....
}
this code works great when the data does NOT contain nasty embedded single-quotes. I'd hoped the binding would solve this problem but no such luck.
Anyone have a solution to this single-quote issue?
Thanks in advance,
Still-learning Steve.