pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jochen Kokemüller <jochen@kokemueller.de>
To: pgsql-sql@postgresql.org
To: pgsql-interfaces@postgresql.org
Subject: Reading bytea field
Date: Fri, 21 Oct 2005 20:46:48 +0200
Message-ID: <43593798.5060707@kokemueller.de> (raw)

Hi All,

sorry for posting another question on bytea fields, i searched in the 
Forum and all over the net and could not find the answer, although it 
seems pretty basic to me.

What i want to to is the follwing:
1. Create a picure in the Linux Shell
2. Insert into a bytea field
3. Retrieve the picture with OpenOffice.org Basic
4. Insert it into a Document.


Step 1 and 4. are not really postgresql relevant.
To solve step 2 i wrote a small c++ programm which inserts the jpeg 
pictue into the bytea field. The relevant part looks like this:

----
      struct stat sbuf;
      stat(file.c_str(), &sbuf);
     
      char fbuf[sbuf.st_size];
     
      int fd = open(file.c_str(), O_RDONLY);

      if (sbuf.st_size!=read(fd, fbuf, sizeof(fbuf)))
    {
      std::cerr << "Error reading: " << argv[1] << std::endl;
    }

      close(fd);
      //unlink(file.c_str());

      Oid in_oid[]={17, 23}; //{1043,17}; /* varchar, bytea */
      const char* params[]={fbuf, nummern.at(i).c_str()};//{pkey,buf};
      const int params_length[]={sizeof(fbuf), 
nummern.at(i).size()};//{strlen(pkey),size};
      const int params_format[]={1,0};//{0,1}; /* text,binary */
      res = PQexecParams(conn,
             "UPDATE Artikel SET barcode=$1 WHERE nummer=$2",
             2,        /* one param */
             in_oid, params, params_length,
             params_format, 1);

      if (res && PQresultStatus(res)==PGRES_COMMAND_OK) {
    std::cout << "Glück" << std::endl;
      }
      else
    {
      std::cout << PQresultErrorMessage(res) << std::endl;
    }

----

I read that it is better to use PQexecParams instead of PQescape. In the 
bytea field is then the picture with every Byte escaped. So the first 
few bytes look like this:

 \377\330\377\340\000\020JFIF ...

I wouldn't bother if i knew how to unescape this sequence. I supposed 
that this is more or less an internal representation, but when i try to 
retrieve the data. i get it like this.

If, on the other hand, i encode the bytea to a text field, the result 
looks nearly good. Just the \000 character stays the same, and maybe the 
three others that have to be escaped.

Isn't there a way to just extract from the bytea field that what i put 
in there?

Thanks for any help in advance,
Jochen






view thread (2+ messages)  latest in thread

Message-ID: <43593798.5060707@kokemueller.de>
Permalink:  ../43593798.5060707@kokemueller.de/
Also on:    postgresql.org/message-id/43593798.5060707@kokemueller.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-interfaces@postgresql.org
  Cc: jochen@kokemueller.de, pgsql-sql@postgresql.org
  Subject: Re: Reading bytea field
  In-Reply-To: <43593798.5060707@kokemueller.de>

* 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