Received: from postgresql.org.org (webmail.postgresql.org [216.126.85.28]) by postgresql.org (8.11.3/8.11.1) with SMTP id f5MCdca49350 for ; Fri, 22 Jun 2001 08:39:38 -0400 (EDT) (envelope-from pgsql-sql-owner@postgresql.org) Received: from news.tht.net (news.hub.org [216.126.91.242]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f5LLVga81860 for ; Thu, 21 Jun 2001 17:31:42 -0400 (EDT) (envelope-from news@news.tht.net) Received: (from news@localhost) by news.tht.net (8.11.4/8.11.4) id f5LLTIB23294 for pgsql-sql@postgresql.org; Thu, 21 Jun 2001 17:29:18 -0400 (EDT) (envelope-from news) From: "Hugh Mandeville" X-Newsgroups: comp.databases.postgresql.sql Subject: Re: Re: binary data Date: Thu, 21 Jun 2001 17:29:41 -0400 Organization: Hub.Org Networking Services (http://www.hub.org) Lines: 55 Message-ID: <9gtovc$mnr$1@news.tht.net> References: Reply-To: "Hugh Mandeville" X-Complaints-To: scrappy@hub.org X-Newsreader: Microsoft Outlook Express 5.50.4133.2400 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 To: pgsql-sql@postgresql.org X-Archive-Number: 200106/361 X-Sequence-Number: 3648 "Alex Pilosov" 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 ..."