pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Hugh Mandeville <hughmandeville@hotmail.com>
To: pgsql-sql@postgresql.org
Subject: Re: Re: binary data
Date: Thu, 21 Jun 2001 17:29:41 -0400
Message-ID: <9gtovc$mnr$1@news.tht.net> (raw)
References: <Pine.BSO.4.10.10106201342060.20262-100000@spider.pilosoft.com>


"Alex Pilosov" <alex@pilosoft.com> wrote in message
news:Pine.BSO.4.10.10106201342060.20262-100000@spider.pilosoft.com...
> Yes, there is a binary datatype, called 'bytea', which can handle anything
> you can throw at it, including nulls.
>
> The rules for escaping things you want to throw at it are tricky though.
> (and same for unescaping things you get back from database).

i ended up having to use the C large object functions to write binary data.
i wasn't able to insert nulls using the INSERT command with psql, C or Perl.
what is the correct way to escape 0?  \000 doesn't work for me.

i created a table with a 'bytea' column. i inserted data escaping out
special characters with their 3 digit octet.
everything inserts correctly except null (\000). data with null characters
inserted with no errors but doing a select for the value or for its size
came back incorrectly.  it returns everything before the null character.
i tried inserting and selecting the data with psql and the C interface
(PQexec, PQgetvalue, PQgetlength).

here is a log from my psql session.  the last insert i did 'null \000 null'
comes back as octet length 5 which is wrong, it should be 11.

test=# CREATE TABLE log (data    bytea);
CREATE
test=# INSERT INTO log (data) VALUES ('plain text');
INSERT 61650 1
test=# INSERT INTO log (data) VALUES ('special chars \n \001 \002');
INSERT 61651 1
test=# INSERT INTO log (data) VALUES ('null \000 null');
INSERT 61652 1
test=# SELECT octet_length(data), data FROM log;
 octet_length |             data
--------------+------------------------------
           10 | plain text
           19 | special chars \012 \001 \002
            5 | null
(3 rows)


> If you are using perl, use DBD::Pg version 1.00 or higher, it encapsulates
> it all transparently.

i also tried doing an insert and using quote() with perl (DBD::Pg v1.0)

$sql = "INSERT INTO log (data) VALUES (" . $dbh->quote($bindata) . ")";

it has the same problem when the data contains a null, perl returns
"DBD::Pg::st execute failed: ERROR:  Unterminated quoted string at ..."








view thread (12+ messages)  latest in thread

Message-ID: <9gtovc$mnr$1@news.tht.net>
Permalink:  ../9gtovc$mnr$1@news.tht.net/
Also on:    postgresql.org/message-id/9gtovc$mnr$1@news.tht.net

 · 

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: hughmandeville@hotmail.com
  Subject: Re: Re: binary data
  In-Reply-To: <9gtovc$mnr$1@news.tht.net>

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

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