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

Re: Loading/Retrieving bytea fields?

From: "Daniel Verite" <daniel(at)manitou-mail(dot)org>
To: "Postgres General" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Loading/Retrieving bytea fields?
Date: 2004-07-28 14:45:10
Message-ID: 20040728164529.5143560@uruguay.brainstorm.fr (view raw or flat)
Thread:
Lists: pgsql-general
	 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.

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

In response to

pgsql-general by date

Next:From: Chris BrowneDate: 2004-07-28 15:15:21
Subject: Re: NNTP server
Previous:From: Mike NolanDate: 2004-07-28 14:40:57
Subject: Re: Before/After trigger sequencing questiont

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