C: libpq-fe 尝试选择 bigint (int8)

标签 c postgresql libpq

我正在尝试在 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而不是这些“非数组”。

喜欢the documentation说:

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/

相关文章:

linux - g++ 抛出文件无法识别 : File format not recognized error

C4047 - 间接级别 - 结构初始化

c++ - gcc 选项 "-fmessage-length"是什么意思?

c++ - "Deep"头部依赖分析

cmake - FindPostgreSQL.cmake 无法在 ubuntu 上运行

sql - 结合嵌入式 SQL 和 libpq 的功能

android - C: 如何在 android 中解析 XML?

sql - 无法从 bash 运行 postgresql 脚本

mysql - 使用 PGSQL() 将 postgresql 与 Asterisk 连接

sql - 行创建时间戳在表中是否唯一?