473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

PQunescapeBytea


The first part of the following piece of code should, and
does, take an array of 5 characters and convert it to an
escaped string of 10 characters (including the terminating
byte).

However, when going in the other direction PQunescapeBytea
reports (in unescaped_len) that the resulting binary data
has a length of 8 instead of 5 and is different than a[].
Am I using the escape/unescape functions incorrectly? I was
expecting the unescape function to produce an exact
duplicate of a[].

Thanks,
Iker

========================
unsigned char a[5];
a[0] = 'a';
a[1] = 'a';
a[2] = 'a';
a[3] = 'a';
a[4] = 0;

for (unsigned int i = 0; i < 5; ++i)
printf("%c\n", a[i]);

size_t escaped_len;
size_t unescaped_len;

unsigned char* escaped = PQescapeBytea(a, 5, &escaped_len);
unsigned char* unescaped = PQunescapeBytea(escaped, &unescaped_len);

printf("\n");
printf("unescaped_len: %d\n", unescaped_len);
for (unsigned int i = 0; i < unescaped_len; ++i)
printf("%c\n", unescaped[i]);

========================
OUTPUT:

a
a
a
a
@ <== this is an unprintable character

unescaped_len: 8
a
a
a
a
\
0
0
0

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 22 '05 #1
1 4014
Iker Arizmendi wrote:
Am I using the escape/unescape functions incorrectly? I was
expecting the unescape function to produce an exact
duplicate of a[].


The two functions are not, and should not be, precise reciprocals.
PQescapeBytea() prepares a string to be used in an SQL statement,
meaning it must produce something in the form "\\ooo" (without the
quotes) -- notice *two* backslashes before the octal number. This is
needed because the SQL string literal parser eats one backslash, and the
byteain() function needs the other to recognize that it is receiving an
octal sequence.

PQunescapeBytea(), on the other hand, takes a string in the form "\ooo"
which is the output format produced by byteaout() function. Notice that
only *one* backslash is expected before the octal.

In your example, "aaaa\0" is fed into PQescapeBytea(), producing
"aaaa\\000". When that is fed into PQunescapeBytea(), the two
backslashes are seen as a single backslash that has been escaped.
Therefore the two backslashes are transformed into a single unescaped
backslash, and the octal number is taken as a literal part of the
string, producing "aaaa\000".

HTH,

Joe

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: bmay2068 | last post by:
I'm currently trying to insert binary data into postgres using the ecpg interface and standard embedded sql. I have a table defined as... CREATE TABLE bintest ( fsize numeric, bindata ...
1
by: Jerry LeVan | last post by:
Has anyone found some C code that shows how to load/extract data from a bytea column? thanks Jerry ---------------------------(end of broadcast)--------------------------- TIP 7: don't...
1
by: Jerry LeVan | last post by:
Is there any chance that PQunescapeBytea will always have a \000 at the end of the unescaped buffer? unescvalue = PQgetvalue(lastResult, row, fieldNum); value = PQunescapeBytea(unescvalue,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.