Re: storing binary data

From: Joe Conway <joseph(dot)conway(at)home(dot)com>
To: Cristóvão Dalla Costa <cbraga(at)bsi(dot)com(dot)br>
Cc: pgsql-interfaces(at)postgresql(dot)org
Subject: Re: storing binary data
Date: 2001-11-14 01:53:27
Message-ID: 3BF1CE97.6040105@home.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces

Cristóvão Dalla Costa wrote:

> Hi,
>
> I'm using libpq to interface my program to postgresql, and I need to store
> some binary data (around 30-40 bytes per element). I'm not sure of the best
> way to do so, since blobs seem awkward. How would you do it? How would you
> insert it in a text field?
>
> Thanks all,
>
> Cristovao
>

I recommend BYTEA datatype for this. It works in a similar manner to
TEXT, but it is designed for binary data. You will need to escape
certain values on INSERT and unescape on SELECT. See:

http://fts.postgresql.org/db/mw/msg.html?mid=1042181

for a recent post on this.

Hope this helps,

-- Joe

p.s.
Note that in 7.2 (in beta now) there is a function in libpq specifically
for escaping BYTEA input strings (PQescapeBytea -- see fe-exec.c):

/*
* PQescapeBytea - converts from binary string to the
* minimal encoding necessary to include the string in an SQL
* INSERT statement with a bytea type column as the target.
*
* The following transformations are applied
* '\0' == ASCII 0 == \\000
* '\'' == ASCII 39 == \'
* '\\' == ASCII 92 == \\\\
*/
unsigned char *
PQescapeBytea(unsigned char *bintext, size_t binlen, size_t *bytealen)

In response to

Browse pgsql-interfaces by date

  From Date Subject
Next Message Alexaki Sofia 2001-11-14 13:46:26 how to use ODBC interface (fwd)
Previous Message Alex Pilosov 2001-11-14 00:08:49 Re: storing binary data