agora inbox for pgsql-sql@postgresql.org  
help / color / mirror / Atom feed
From: Christoph Haller <ch@rodos.fzk.de>
To: pgsql-sql@postgresql.org
Cc: achong@cecid.hku.hk
Subject: Re: bytea
Date: Wed, 05 Feb 2003 11:15:55 +0100
Message-ID: <3E40E45A.6D20C6EE@rodos.fzk.de> (raw)

>
> I have a table containing a field of type bytea:
>
> CREATE TABLE a_table (
>     a_field bytea
> );
>
> How can I import a file in a SQL script? What function I can use?
>
The documentation says as in PostgreSQL 7.2.1 (I doubt this changed
significantly since)

Octets of certain values must be escaped (but all octet values may be
escaped) when used as part of a string literal in an SQL
statement. In general, to escape an octet, it is converted into the
three-digit octal number equivalent of its decimal octet value, and
preceded by two backslashes.

In general it goes like this
INSERT INTO a_table ( a_field ) VALUES ( '\\000\\001\\002\\003' ) ;
to load the first four ASCII characters.
You did not mention how your file looks like.
There is also a C function available called PQescapeBytea
which does all the required escaping to store memory areas in bytea
columns.
Refer to Command Execution Functions within libpq - C Library for
details.

Regards, Christoph





view thread (17+ messages)  latest in thread

Message-ID: <3E40E45A.6D20C6EE@rodos.fzk.de>
Permalink:  ../3E40E45A.6D20C6EE@rodos.fzk.de/
Also on:    postgresql.org/message-id/3E40E45A.6D20C6EE@rodos.fzk.de

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: ch@rodos.fzk.de, achong@cecid.hku.hk
  Subject: Re: bytea
  In-Reply-To: <3E40E45A.6D20C6EE@rodos.fzk.de>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox