我使用libpq
在表中插入浮点数。我得到这个错误INSERT failed: ERROR: insufficient data left in message
。使用libpq在表中插入浮点数
这里是我的代码库对应的摘录:
printf ("Enter write parameter_value");
scanf("%f", ¶meter_value);
char *stm = "INSERT INTO write_reg_set (parameter_value) VALUES ($1::double precision)";
int nparam = 1;
//set the values to use
const char *values[1] = {(char *)¶meter_value};
//calculate the lengths of each of the values
int lengths[1] = {sizeof(parameter_value)};
//state which parameters are binary
int binary[1] = {1};
PGresult *res = PQexecParams(conn,
stm,
nparam, //number of parameters
NULL, //ignore the Oid field
values, //values to substitute $1 and $2 and so on
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
if (PQresultStatus(res) != PGRES_COMMAND_OK) {
fprintf(stderr, "INSERT failed: %s", PQerrorMessage(conn));
exit_nicely(conn,res);
}
PQclear(res);
第一点:testlibpq2.c说ergarding OID“让后端推断参数类型”为二进制和文本的例子。这就是我省略它。你的第二点是有效的。 – Jam
你认为后端应该推断出你传递的4个字节是双精度? [文档](https://www.postgresql.org/docs/current/static/libpq-exec.html#LIBPQ-PQEXECPARAMS)说:*如果paramTypes为NULL,或者数组中的任何特定元素为零,则服务器推断参数符号的数据类型的方式与它对**无类型文字字符串**。(强调我的) –