libpq, PQExecParams and the inserting of binary data

From: David Hinkle <drachs(at)gmail(dot)com>
To: pgsql-interfaces(at)postgresql(dot)org
Subject: libpq, PQExecParams and the inserting of binary data
Date: 2005-06-03 16:18:25
Message-ID: ae830e6c05060309182201f85b@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces

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...

Responses

Browse pgsql-interfaces by date

  From Date Subject
Next Message Volkan YAZICI 2005-06-03 16:43:34 Re: libpq, PQExecParams and the inserting of binary data
Previous Message Volkan YAZICI 2005-06-03 13:55:51 Re: libpq, blocking/nonblocking mechanism