问题
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