I've read the document but still I'm confused how use this function to send this command to server:
INSERT INTO table_1($1,$2,$3);
this function I wrote:
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
);
}
but during the compile this error occurs:
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 describes 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);
The parameter values are not the fourth, but the fifth argument.
The fourth argument, if not left NULL
, is an array with the object IDs describing the types of the parameters.
Note that in C, a string like "me"
does not occupy two, but three bytes (you have to include the final zero byte).