sql - 如何在 libpq 中使用 PQexecParams?

标签 sql c database postgresql libpq

我已经阅读了文档,但我仍然很困惑如何使用此函数将此命令发送到服务器:

INSERT INTO table_1($1,$2,$3);
我写的这个函数:
void add_data_to_db(char table){
        const char data[2][2] = {"12","me"};
        re = PQexecParams(connection,
                        "INSERT INTO test_table($1,$2)",
                        2,data,NULL,NULL,NULL,0
                        );
}
但是在编译过程中会出现这个错误:
db.c: In function ‘add_data_to_db’:
db.c:40:6: warning: passing argument 4 of ‘PQexecParams’ from incompatible pointer type [-Wincompatible-pointer-types]
   40 |    2,data,NULL,NULL,NULL,0
      |      ^~~~
      |      |
      |      const char (*)[2]
In file included from db.c:2:
/usr/include/libpq-fe.h:391:18: note: expected ‘const Oid *’ {aka ‘const unsigned int *’} but argument is of type ‘const char (*)[2]’
  391 | extern PGresult *PQexecParams(PGconn *conn,
      |                  ^~~~~~~~~~~~

最佳答案

The documentation描述 PQexecParams :

PGresult *PQexecParams(PGconn *conn,
                       const char *command,
                       int nParams,
                       const Oid *paramTypes,
                       const char * const *paramValues,
                       const int *paramLengths,
                       const int *paramFormats,
                       int resultFormat);

参数值不是第四个,而是第五个参数。
第四个参数,如果没有留下 NULL , 是一个包含描述参数类型的对象 ID 的数组。
请注意,在 C 中,像 "me" 这样的字符串不占用两个,而是三个字节(你必须包括最后的零字节)。

关于sql - 如何在 libpq 中使用 PQexecParams?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62666456/

相关文章:

sql - 在 SQL Server 中存储 zip

sql - 如何在特定时间范围内计算任何 N 秒持续时间的 SQL MAX()

c - 制作哈希金字塔

c++ - 为什么 C/C++ 中没有 ^^ 运算符?

mysql: show databases 不显示任何数据库

mysql - 如何编写一个 mysql 查询,从一个表读取数据并写入另一个表?

MySQL - 从条目中删除子字符串

c++ - 文件描述符,printf 和 std::cout 的区别

ruby-on-rails - 出于性能原因预先检索代码中的所有对象

php - 如何访问 htdocs 文件夹之外的 XAMPP mysql 数据库