On Fri, September 15, 2006 00:17, Adam Kubach wrote:
> I'm using libpqxx 2.5.5 and I'm trying to retrieve binary data from a
> database.  What's the best way to do this?

Before anything else, I'd consider upgrading to something more recent,
such as 2.6.8.  It's been a long, long time since 2.5.5 so I'm not sure
everything I'll say here applies to that version.


>  I'm using the c_str function,

Don't do that!  c_str() returns a C-style string.  Which means that the
data ends at the first nul byte.  Binary data can generally have nul bytes
embedded in it, and you don't want your data to be truncated when that
happens.


> but I'm getting '\' in the data that aren't there.  Are there any methods
> to get the raw data from the sql query?

Those are there to escape characters that can't be represented as text. 
The database deals primarily in text, not in arbitrary strings of bytes.

You have two options for dealing with binary data:

1. If the values can get quite large, use the largeobject class family. 
These work much like files.

2. Otherwise, create columns of BYTEA type and use the binarystring class
to retrieve data and un-escape it.  To write binary data, use the
transaction's esc_raw() function.


Jeroen


_______________________________________________
Libpqxx-general mailing list
[email protected]
http://gborg.postgresql.org/mailman/listinfo/libpqxx-general

Reply via email to