469,599 Members | 2,595 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,599 developers. It's quick & easy.

Loading/Retrieving bytea fields?

Has anyone found some C code that shows how to load/extract data from a
bytea column?

thanks

Jerry
---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings

Nov 23 '05 #1
1 3209
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

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by mPath Records | last post: by
2 posts views Thread by Alvar Freude | last post: by
reply views Thread by Alvar Freude | last post: by
4 posts views Thread by David Garamond | last post: by
7 posts views Thread by C G | last post: by
6 posts views Thread by Egyd Csaba | last post: by
1 post views Thread by Jerry LeVan | last post: by
4 posts views Thread by Jerry LeVan | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.