PostgreSQL/PostGIS - PQexecParams - wrong element type

折月煮酒 提交于 2021-01-27 17:26:58

问题


I have this code.

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));

where psql is connection to PostgreSQL with PostGIS. However, when I run this code, I got "ERROR: wrong element type"

What is causing this? OID should be deduced by database from specified ::double precision[][]


回答1:


The problem is that the internal binary format of a double precision[] is quite different from a C array.

If you really don't want to use the text format, you'll have to construct the internal binary array representation yourself.

See include/server/utils/array.h for details. Since you cannot link with the PostgreSQL server in client code, you cannot use the utility functions from that file. Also consider that the internal binary representation of a double precision might be different on client and server if the machines have different architecture.

All in all, you are usually much better off using the text format.



来源:https://stackoverflow.com/questions/57810422/postgresql-postgis-pqexecparams-wrong-element-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!