Myt*_*ium 2 postgresql perl dbi
我试图在pSQL表中插入一行,指定键和值作为占位符:
my @keys = keys %db_entr;
my @vals = values %db_entr;
my @items = (@keys, @values);
my $dbh = DBI->connect("DBI:Pg:dbname=testdb;host=localhost", "username", 'password', {'RaiseError' => 1});
my $sth = $dbh->prepare("INSERT INTO grid ( ?, ?, ? ) values ( ?, ?, ? )");
my $rows = $sth->execute(@items);
print "$rows effected\n";
Run Code Online (Sandbox Code Playgroud)
但是,无论我做什么,这都给了我一个错误:
DBD::Pg::st execute failed: ERROR: syntax error at or near "$1"
LINE 1: INSERT INTO grid ( $1, $2, ...
^ at ./file.pl line 262, <STDIN> line 11.
Run Code Online (Sandbox Code Playgroud)
有没有人知道我可能做错了什么?
您不能使用占位符作为列名,如下所示:
INSERT INTO grid (?, ?, ?) VALUES (?, ?, ?)
Run Code Online (Sandbox Code Playgroud)
您必须明确指定列名称,并且只能使用占位符作为值:
INSERT INTO grid (x, y, z) VALUES (?, ?, ?)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3217 次 |
| 最近记录: |