Thread: Re: [DEFAULT] Daily digest v1.4551 (20 messages)

Re: [DEFAULT] Daily digest v1.4551 (20 messages)

From
Jerry LeVan
Date:
Thank you Daniel,

Is it the case that "insert" could be used in a similiar
fashion to load the bytea field?

Jerry

On Jul 28, 2004, at 3:07 PM,Daniel Verite wrote:

>
> Date: Wed, 28 Jul 2004 16:45:10 +0200
> From: "Daniel Verite" <daniel@manitou-mail.org>
> To: "Postgres General" <pgsql-general@postgresql.org>
> Subject: Re: Loading/Retrieving bytea fields?
> Message-ID: <20040728164529.5143560@uruguay.brainstorm.fr>
>
>      Jerry LeVan writes
>
>> Has anyone found some C code that shows how to load/extract data from
>> a
>> bytea column?
>
> This works for me:
>
> int size;
> const char* contents;
> PGresult* res;
> res = PQexecParams(pgconn,
>         "SELECT contents FROM tblob WHERE pkey=value",
>         0, NULL,NULL,NULL,NULL,   /* no input parameters */
>         1  /* output in binary format */ );
>
> if (res && PQresultStatus(res)==PGRES_TUPLES_OK) {
>   size = PQgetlength(res, 0, 0);
>   contents = PQgetvalue(res, 0, 0);    /* binary representation */
> }
>
> I believe that you may also use the plain PQexec instead of
> PQexecParams,
> but then you would have to call PQunescapeBytea() on the bytea column
> in the
> results, so it's likely to be less efficient. I haven't tried that,
> though.
>


Re: [DEFAULT] Daily digest v1.4551 (20 messages)

From
"Daniel Verite"
Date:
         Jerry LeVan writes

> Is it the case that "insert" could be used in a similiar
> fashion to load the bytea field?

Here's a code snippet for inserting a (varchar,bytea) tuple.

int write_bytea(const char* pkey, const char* buf, int size)
{
 Oid in_oid[]={1043,17}; /* varchar, bytea */
 const char* params[]={pkey,buf};
 const int params_length[]={strlen(pkey),size};
 const int params_format[]={0,1}; /* text,binary */
 PGresult* res;
 res = PQexecParams(pgconn,  
                    "INSERT INTO tblob(pkey,contents) VALUES ($1,$2)",  
                    sizeof(params)/sizeof(params[0]),
                    in_oid, params, params_length,
                    params_format, 1);
 if (res && PQresultStatus(res)==PGRES_COMMAND_OK) {
  /* success */
 }
}

--
 Daniel
 PostgreSQL-powered mail user agent and storage: http://www.manitou-mail.org