我想将整数数组发布到我的postgresql数据库中。我知道我可以将所有格式都格式化为一个字符串,然后将该字符串作为一个SQL命令发送。但是,我相信PQexecParams函数也应该带来一些帮助。但是,我很遗憾,因为如何使用它。在C中使用postgresql插入整数数组(libpq)
//we need to convert the number into network byte order
int val1 = 131;
int val2 = 2342;
int val3[5] = { 0, 7, 15, 31, 63 };
//set the values to use
const char *values[3] = { (char *) &val1, (char *) &val2, (char *) val3 };
//calculate the lengths of each of the values
int lengths[3] = { sizeof(val1), sizeof(val2), sizeof(val3) * 5 };
//state which parameters are binary
int binary[3] = { 1, 1, 1 };
PGresult *res = PQexecParams(conn, "INSERT INTO family VALUES($1::int4, $2::int4, $3::INTEGER[])", 3, //number of parameters
NULL, //ignore the Oid field
values, //values to substitute $1 and $2
lengths, //the lengths, in bytes, of each of the parameter values
binary, //whether the values are binary or not
0); //we want the result in text format
是的,这是从一些教程复制。 然而,这将返回:
ERROR: invalid array flags
使用传统的方法做工作:
PQexec(conn, "INSERT INTO family VALUES (2432, 31, '{0,1,2,3,4,5}')");
插入数据就好了,我可以读出来罚款以及。
任何帮助将不胜感激! :)
postgresql文档(http://www.postgresql.org/docs/9.4/static/libpq-exec.html#LIBPQ-EXEC-MAIN)表示即使对于二进制数据,也可以通过后缀参数符号来避免OID类型(例如'$ 1 :: bigint')。这是错的吗? –
@undur_gongor它是正确的文本格式。不知道二进制。 –