尝试使用 Perl 和 DBI 在 PostgreSQL 中设置一个值,从而使用 DBD::Pg。
我遇到了一个奇怪的错误。
2013-05-23 19:02:36.641139500 updating status to 0
2013-05-23 19:02:36.641410500 DBD::Pg::st execute failed: ERROR: syntax error at or near "$1"
2013-05-23 19:02:36.641418500 LINE 1: UPDATE instances SET $1 = $2
2013-05-23 19:02:36.641423500 ^ at /usr/lib/perl5/vendor_perl/Mitel/MslRest/mbg.pm line 161.
2013-05-23 19:02:36.642425500 [Thu May 23 19:02:36 2013] [error] DBD::Pg::st execute failed: ERROR: syntax error at or near "$1"
2013-05-23 19:02:36.642438500 LINE 1: UPDATE instances SET $1 = $2
2013-05-23 19:02:36.642443500 ^ at /usr/lib/perl5/vendor_perl/Mitel/MslRest/mbg.pm line 161.
2013-05-23 19:02:36.642447500
相关代码是
my $sql = "UPDATE instances SET ? = ?";
my $dbh = Mitel::tug::getdbh();
$dbh->begin_work;
my $sth = $dbh->prepare($sql);
unless ($sth) {
return $self->internal_error("prepare failed: " . $dbh->errstr);
}
foreach my $propname (sort keys %{ $raw_data }) {
my $propval = $raw_data->{$propname};
print STDERR "updating $propname to $propval\n";
if (! $sth->execute($propname, $propval)) {
$dbh->rollback;
$sth->finish;
return $self->internal_error("execute: " . $dbh->errstr);
}
}
$dbh->commit;
$sth->finish;
所以我试图将“状态”更新为 0,使用执行方法来防止 sql 注入,但由于某种原因我遇到了语法错误。
有人遇到这个吗?
root@miketug2 ~]# perl -v
This is perl, v5.10.1 (*) built for i386-linux-thread-multi
perl-DBD-Pg-2.15.1-4.el6_3.i686
postgresql84-server-8.4.14-1PGDG.rhel6.i686
root@miketug2 ~]# uname -a
Linux miketug2 2.6.32-279.22.1.el6.i686 #1 SMP Wed Feb 6 00:31:03 UTC 2013 i686 i686 i386 GNU/Linux