Skip site navigation (1) Skip section navigation (2)

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 (view raw or flat)
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

pgsql-interfaces by date

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

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group