pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: David Hinkle <drachs@gmail.com>
To: pgsql-interfaces@postgresql.org
Subject: libpq, PQExecParams and the inserting of binary data
Date: Fri, 3 Jun 2005 11:18:25 -0500
Message-ID: <ae830e6c05060309182201f85b@mail.gmail.com> (raw)

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



view thread (18+ messages)  latest in thread

Message-ID: <ae830e6c05060309182201f85b@mail.gmail.com>
Permalink:  ../ae830e6c05060309182201f85b@mail.gmail.com/
Also on:    postgresql.org/message-id/ae830e6c05060309182201f85b@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-interfaces@postgresql.org
  Cc: drachs@gmail.com
  Subject: Re: libpq, PQExecParams and the inserting of binary data
  In-Reply-To: <ae830e6c05060309182201f85b@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox