c++ - 通过 libpqxx 将数据插入 PostgreSQL 时出现问题

标签 c++ database postgresql libpqxx

我有一个名为 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/

相关文章:

c++ - 从 32 位应用程序 Hook 64 位进程

c++ - 关于在我的应用程序中实现 QThread 的问题?

asp.net-mvc - ASP.NET MVC : HOWTO: Update database after editing multiselectlist (listbox)

postgresql - 对大型空间选择进行排序不使用 GiST 索引(Postgres 11.5)

c++ - 如何在一个代码库中支持具有不同命名空间的两个版本的第三方库

c++ - 对重载函数的模糊调用 - int 和 int&

php - 如何按两行的划分对sql输出进行排序?

php - PDO 和多查询/并发问题

postgresql - 从postgis中的一个点周围的点创建一个多边形

postgresql - 如何使用 PostgreSQL 在 Ecto 中使用点列类型