471,851 Members | 1,219 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,851 software developers and data experts.

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 3267
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
NeoPa
reply views Thread by NeoPa | last post: by
reply views Thread by YellowAndGreen | last post: by
aboka
reply views Thread by aboka | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.