我已经阅读了文档,但我仍然很困惑如何使用此函数将此命令发送到服务器:
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/