我有一个 Perl 脚本,它构建了一个 sql cmd 以将 MS Access db 中某个表中的某些字段设置为 NULL(抱歉)。这是一个简化的模型。
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
....
}
当数据不包含讨厌的嵌入式单引号时,此代码效果很好。我希望绑定能解决这个问题,但没有这样的运气。
有人有解决这个单引号问题的方法吗?
提前致谢,
仍在学习史蒂夫。