Reputation: 11
I am newbie in postgresql and trying to insert the values in db with PQexecparams. When i bind two parameters in values array it works perfectly but when i move to three it shows an error "INSERT failed: cannot allocate memory for output buffer" Here is my code:
void InsertBinaryRec(PGconn *conn, double *valueX, char *nameString, double *valueY)
{
int paramLengths[10];
int paramFormats[3] = { 1, 0, 1 };
const char* values[3] = {(const char *) valueX, nameString, (const char *) valueY };
cout << "ya phr gya????" << endl;
paramLengths[3] = 10 ;
PGresult *res = PQexecParams(conn,
"INSERT INTO testData (X, NAME, Y) VALUES ($1::bytea, $2::TEXT, $3::bytea)",
3,
NULL,
values,
paramLengths,
paramFormats,
3);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "INSERT failed: %s\n", PQerrorMessage(conn));
PQclear(res);
CloseConn(conn);
}
cout << "Insert testData record - OK\n";
PQclear(res);
}
Upvotes: 0
Views: 714
Reputation: 247545
You should initialize paramLengths
like this:
int paramLengths[3] = { (int)sizeof(double), 0, (int)sizeof(double) };
It is rather weird to store the binary representation of a double precision value as a bytea
field, but if you don't need to process it in the database, why not. That way you won't lose precision.
Upvotes: 1
Reputation: 324841
paramLengths[3] = 10;
You've assigned an unused entry in paramLengths
, and left the two entries for your two binary mode params uninitialized.
You're also assuming that your host's native format for double
is the same as PostgreSQL's wire format for binary double
. You need to make very sure that's the case, or convert to text-format. If you are going to pass double*
values directly in the values array, you should at least set the corresponding lengths fields to sizeof(double)
.
Upvotes: 0