如何在libpq中使用pqexecparams?

jjjwad0x  于 2021-08-01  发布在  Java
关注(0)|答案(1)|浏览(460)

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

  1. INSERT INTO table_1($1,$2,$3);

我写的这个函数:

  1. void add_data_to_db(char table){
  2. const char data[2][2] = {"12","me"};
  3. re = PQexecParams(connection,
  4. "INSERT INTO test_table($1,$2)",
  5. 2,data,NULL,NULL,NULL,0
  6. );
  7. }

但在编译过程中,会出现以下错误:

  1. db.c: In function add_data_to_db’:
  2. db.c:40:6: warning: passing argument 4 of PQexecParams from incompatible pointer type [-Wincompatible-pointer-types]
  3. 40 | 2,data,NULL,NULL,NULL,0
  4. | ^~~~
  5. | |
  6. | const char (*)[2]
  7. In file included from db.c:2:
  8. /usr/include/libpq-fe.h:391:18: note: expected const Oid *’ {aka const unsigned int *’} but argument is of type const char (*)[2]’
  9. 391 | extern PGresult *PQexecParams(PGconn *conn,
  10. | ^~~~~~~~~~~~
wixjitnu

wixjitnu1#

文档描述 PQexecParams :

  1. PGresult *PQexecParams(PGconn *conn,
  2. const char *command,
  3. int nParams,
  4. const Oid *paramTypes,
  5. const char * const *paramValues,
  6. const int *paramLengths,
  7. const int *paramFormats,
  8. int resultFormat);

参数值不是第四个参数,而是第五个参数。
第四个论点,如果不是左 NULL ,是一个具有描述参数类型的对象ID的数组。
注意,在c中,字符串 "me" 不占用两个字节,但占用三个字节(必须包含最后的零字节)。

相关问题