【问题标题】:PostgreSQL/PostGIS - PQexecParams - wrong element typePostgreSQL/PostGIS - PQexecParams - 错误的元素类型
【发布时间】:2019-09-06 08:46:36
【问题描述】:

我有这个代码。

const char * q = "INSERT INTO test_raster (raw_data) VALUES 
    (ST_SetValues(ST_AddBand(ST_MakeEmptyRaster(2, 2, -180, -90, 180, -90, 0, 0, 4326), 
     1, '8BUI', 0, 0), 1, 1, 1, $1::double precision[][]))";

double tmp[2][2] = { {0, 125}, {45, 255} };

const char *values[1] = { (char *)&tmp };
int lengths[1] = { 4*sizeof(double) };
int binary[1] = { 1 };

PGresult *rs = PQexecParams(psql,
    q,
    1,
    NULL,
    values,
    lengths, 
    binary,
    0);

auto stat = PQresultStatus(rs);

printf("%s", PQresultErrorMessage(rs));

psql 是通过 PostGIS 连接到 PostgreSQL。但是,当我运行这段代码时,我得到了"ERROR: wrong element type"

这是什么原因造成的? OID应该由数据库从指定的::double precision[][]推导出来

【问题讨论】:

    标签: c postgresql postgis


    【解决方案1】:

    问题在于 double precision[] 的内部二进制格式与 C 数组完全不同。

    如果你真的不想使用文本格式,你必须自己构造内部二进制数组表示。

    详情请见include/server/utils/array.h。由于您无法在客户端代码中与 PostgreSQL 服务器链接,因此您无法使用该文件中的实用程序函数。还要考虑如果机器具有不同的架构,double precision 的内部二进制表示在客户端和服务器上可能会有所不同。

    总而言之,使用文本格式通常会好得多。

    【讨论】:

    • you'll have to construct the internal binary array representation yourself.我无法在array.h中看到详细信息,因为它只是一个头文件,没有实际的示例/代码。
    • 您必须通过 git grep 浏览源代码才能看到它的使用情况。我向你展示了定义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-26
    • 2017-06-29
    • 2011-10-23
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多