我正在尝试在 postgres 上运行一个简单的查询,但无法获取 bigint
列的值。
我尝试的所有操作都返回 0
(零)或错误的值...
当我使用strtoll
时,返回0
(零)
int64_t ret = -1;
ret = strtoll(v_c3_01, NULL, 10);
使用的测试代码是...
PGresult *res = PQexecParams(
conn,
"SELECT c3_01::int8, c3_02::text FROM v_acs_3_01",
0, NULL, NULL, NULL, NULL,
1);
int NC_c3_01 = PQfnumber(res, "c3_01");
int NC_c3_02 = PQfnumber(res, "c3_02");
for (int i = 0; i < PQntuples(res); i++)
{
char *v_c3_01, *v_c3_02;
v_c3_01 = PQgetvalue(res, i, NC_c3_01);
v_c3_02 = PQgetvalue(res, i, NC_c3_02);
// Return 0(zero)
int64_t tst1 = -1;
tst1 = strtoll(v_c3_01, NULL, 10);
printf("%s \t %ld \t %s\n", v_c3_01, tst1, v_c3_02);
}
如果有人已经通过了此测试,或者现在我做错了什么,请帮助我。
谢谢
已解决
uint64_t uid = 0;
uid = be64toh(*(uint64_t*)(v_c3_01));
printf("%ld\n", uid);
最佳答案
您应该传递NULL
而不是这些“非数组”。
Binary representations for integers use network byte order (most significant byte first).
因此,您应该使用 include/port/pg_bswap.h
中定义的 pg_ntoh64
宏。
但从结果集中提取数字的方式可能也存在错误;你没有显示那段代码。
关于C: libpq-fe 尝试选择 bigint (int8),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48160577/