Reading bytea field - Mailing list pgsql-sql

From Jochen Kokemüller
Subject Reading bytea field
Date
Msg-id 43593798.5060707@kokemueller.de
Whole thread Raw
List pgsql-sql
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





pgsql-sql by date:

Previous
From: Tom Lane
Date:
Subject: Re: Delete rule chain stops unexpectedly
Next
From: Wiebe Cazemier
Date:
Subject: Re: Delete rule chain stops unexpectedly