I have been trying to figure this out all morning, and I've gotten no
where so far.
I am trying to insert binary data into a bytea column with
PQExecParams. I couldn't find any documentation on using PQExecParams
with binary parameters, do I tried to do it this way:
int _ma_logbody(struct MailData *MailData, char *bodyp, size_t bodylen) { const char *paramValues[2]; char
*text_body; PGresult *res; size_t newlen;
text_body = PQescapeBytea(bodyp, bodylen, &newlen);
paramValues[0] = MailData->MsgId; paramValues[1] = text_body;
res = PQexecParams(conn, "insert into ma_body (msg_id, body)
VALUES ($1, $2);", 2, /* params */ NULL, /* let the backend deduce param type */
paramValues, NULL, /* don't need param lengths since text */ NULL, /* default to all text
params*/ 0);
PQfreemem(text_body);
if (PQresultStatus(res) != PGRES_COMMAND_OK) { log(LOG_MAIL, "Postresql insert failed: %s",
PQerrorMessage(conn)); log(LOG_MAIL, "bodylen: %d, bodyp: %s", bodylen, bodyp); PQclear(res);
PQfinish(conn); conn = NULL; return(0); }
PQclear(res);
return(TRUE); }
As you can see, I assumed I could use PQexapeBytea to escape the
binary data and then just use the returned value as a text parameter.
However, I randomly get insert errors with the error message:
"invalid input syntax for type Bytea".
If anybody could help me find the right way to do this I'd really
apreciate it...