problem with escaping "

Started by Andrei Ivanovover 23 years ago2 messagesgeneral
Jump to latest
#1Andrei Ivanov
andrei.ivanov@ines.ro

Hello,
I've created a new type in C, with the following structure:

typedef struct movie_property {
int4 length;
int4 id;
char name[31];
char value[256];
} movie_property;

I did the functions for the input, parsing and output, but one problem
apears.
If I try to insert some text with " inside, I try to escape it with a \
(in fact, I'm doing this from php, with its function "addslashes")

INSERT INTO pp VALUES ('(1, "aa \" bb", "fghij")')

But I get an error:
Bad movie_property external representation '(1, "aa " bb", "fghij")'

which means that my movie_property_in function receives the string
'(1, "aa " bb", "fghij")', without the \ in it.

The same exact thing works for varchar or text fields.
What can I do ?

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Andrei Ivanov (#1)
Re: problem with escaping "

Andrei Ivanov <andrei.ivanov@ines.ro> writes:

If I try to insert some text with " inside, I try to escape it with a \
INSERT INTO pp VALUES ('(1, "aa \" bb", "fghij")')
But I get an error:
Bad movie_property external representation '(1, "aa " bb", "fghij")'
which means that my movie_property_in function receives the string
'(1, "aa " bb", "fghij")', without the \ in it.

Yup. The backslash will be eaten by the string-literal parser.

The same exact thing works for varchar or text fields.

No it doesn't. Observe:

regression=# select ('(1, "aa \" bb", "fghij")')::text;
text
-------------------------
(1, "aa " bb", "fghij")
(1 row)

You'll need to double the backslash if you want the type's I/O function
to see it. Compare for example the discussion of array quoting at the
bottom of this page:
http://www.ca.postgresql.org/users-lounge/docs/7.2/postgres/arrays.html

regards, tom lane