我有一个名为 mydb
的数据库,其中包含一个表 People(id, name)
。
我想使用 pqxx C++ 接口(interface)向该表插入一行。
SQL 查询非常简单 INSERT INTO people (id, name) VALUES (1, "Bob");
C++ 代码在这里:
#include <pqxx/pqxx>
using namespace std;
int main(int, char *argv[])
{
pqxx::connection conn( /* some stuff here */ );
conn.prepare("insert_to_people", "insert into people (id, name) values ($1, $2);");
pqxx::work pq(conn);
pq.prepared("insert_to_people")(1)("Bob").exec();
pq.commit();
return 0;
}
但我遇到以下异常:
terminate called after throwing an instance of 'pqxx::usage_error'
what(): Too many arguments for prepared statement insert_to_people: expected 0, received 2
Aborted (core dumped)
出了什么问题?
最佳答案
我已经好几年没有使用c++了!查看文档似乎需要声明参数类型?也许这会起作用:
conn.prepare("insert_to_people", "insert into people (id, name) values ($1, $2)")("integer")("varchar", pqxx::prepare::treat_string);
此外,还有一个尾随 ;在查询中。我认为这不会造成伤害,但没有必要。
-g
关于c++ - 通过 libpqxx 将数据插入 PostgreSQL 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26612204/