Thread: storing binary data

storing binary data

From
Cristóvão Dalla Costa
Date:
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



Re: storing binary data

From
Michael Lam
Date:
I've seen people converting binary data into base64 or uuencode first before
storing into text files.  Maybe you can do the same and store in a varchar
field if you like too? Hm...

-----Original Message-----
From: Cristovao Dalla Costa [mailto:cbraga@bsi.com.br]
Sent: Tuesday, November 13, 2001 6:10 PM
To: pgsql-interfaces@postgresql.org
Subject: [INTERFACES] storing binary data


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


---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org


Re: storing binary data

From
Joe Conway
Date:
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
anSQL *    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)



Re: storing binary data

From
Alex Pilosov
Date:
use bytea. search archives.

On Tue, 13 Nov 2001, Michael Lam wrote:

> I've seen people converting binary data into base64 or uuencode first before
> storing into text files.  Maybe you can do the same and store in a varchar
> field if you like too? Hm...
> 
> -----Original Message-----
> From: Cristovao Dalla Costa [mailto:cbraga@bsi.com.br]
> Sent: Tuesday, November 13, 2001 6:10 PM
> To: pgsql-interfaces@postgresql.org
> Subject: [INTERFACES] storing binary data
> 
> 
> 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
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
> 
> http://archives.postgresql.org
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly
> 
> 

-- 
--
Alex Pilosov            | http://www.acedsl.com/home.html
CTO - Acecape, Inc.     | AceDSL:The best ADSL in the world
325 W 38 St. Suite 1005 | (Stealth Marketing Works! :)
New York, NY 10018      |