From: | Jochen Kokemüller <jochen(at)kokemueller(dot)de> |
---|---|
To: | pgsql-sql(at)postgresql(dot)org, pgsql-interfaces(at)postgresql(dot)org |
Subject: | Reading bytea field |
Date: | 2005-10-21 18:46:48 |
Message-ID: | 43593798.5060707@kokemueller.de |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-interfaces 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
From | Date | Subject | |
---|---|---|---|
Next Message | Stefan T. Peters | 2005-10-21 19:37:40 | libpq won't connect when client is built with MinGW |
Previous Message | Zlatko Matić | 2005-10-20 10:33:58 | versions of oDBC driver |
From | Date | Subject | |
---|---|---|---|
Next Message | Charles | 2005-10-21 20:17:25 | How much more would you earn? |
Previous Message | Tom Lane | 2005-10-21 16:47:21 | Re: [pgsql-advocacy] Oracle buys Innobase |