Обсуждение: Escaping literal strings in pg_8.4

Поиск
Список
Период
Сортировка

Escaping literal strings in pg_8.4

От
Ennio-Sr
Дата:
Hi all!

[using postgresql_8.4 under debian/GNU-Linux 2.6.32]

I'm trying to understand how the escape_string is supposed to work.
After writing a file ('prova12000.memo')  with this single line:
-----
Vediamo se quest''altro E'\r\n' lo riconosce come un a capo E'\r\n' e questo dovrebbe essere il nuovo rigo: ''bla bla
bla''!
-----
entered the command:

$ psql mydb -c "insert into bibl_memos values(12000, '`cat prova12000.memo`');"

and got:

ERROR:  syntax error at or near "\"
LINE 1: ...bl_memos values(12000, 'Vediamo se quest''altro E'\r\n' lo r...
                                                             ^
Both the lines 'escape_string_warning=on' and 'standard_conforming_strings=off' are commented out in postgresql.conf.

Could somebody please explain what is going wrong?

Thanks for your attention. Regards,
          ennio
--
[Perche' usare Win$ozz (dico io) se ..."anche uno sciocco sa farlo.   \\?//
 Fa' qualche cosa di cui non sei capace!"  (diceva Henry Miller) ]    (°|°)
[Why use Win$ozz (I say) if ... "even a fool can do that.              )=(
 Do something you aren't good at!" (as Henry Miller used to say) ]

Re: Escaping literal strings in pg_8.4

От
Willy-Bas Loos
Дата:
well, you have one single quote before the first backslash. so that's the end of the quoted string. Then comes a backslash, which is bad syntax. So it's the quote, not the backslash that causes the error.

maybe you mean this:
--
Vediamo se quest''altro \r\n lo riconosce come un a capo \r\n e questo dovrebbe essere il nuovo rigo: ''bla bla bla''!
--
psql -c "insert into bibl_memos values(12000, E'`cat prova12000.memo`');"
INSERT 0 1
psql -c "select * from bibl_memos"
  id   |                           val                          
-------+---------------------------------------------------------
 12000 | Vediamo se quest'altro \r                              +
       |  lo riconosce come un a capo \r                        +
       |  e questo dovrebbe essere il nuovo rigo: 'bla bla bla'!
(1 row)

It is kind of hard to get text like that into insert statements.
I would advise to use COPY. It takes TAB delimeted files, or CSV (e.g. from a spreadsheet)

HTH, cheers,

WBL

On Thu, Apr 12, 2012 at 7:45 PM, Ennio-Sr <nasr.laili@tin.it> wrote:
Hi all!

[using postgresql_8.4 under debian/GNU-Linux 2.6.32]

I'm trying to understand how the escape_string is supposed to work.
After writing a file ('prova12000.memo')  with this single line:
-----
Vediamo se quest''altro E'\r\n' lo riconosce come un a capo E'\r\n' e questo dovrebbe essere il nuovo rigo: ''bla bla bla''!
-----
entered the command:

$ psql mydb -c "insert into bibl_memos values(12000, '`cat prova12000.memo`');"

and got:

ERROR:  syntax error at or near "\"
LINE 1: ...bl_memos values(12000, 'Vediamo se quest''altro E'\r\n' lo r...
                                                            ^
Both the lines 'escape_string_warning=on' and 'standard_conforming_strings=off' are commented out in postgresql.conf.

Could somebody please explain what is going wrong?

Thanks for your attention. Regards,
         ennio
--
[Perche' usare Win$ozz (dico io) se ..."anche uno sciocco sa farlo.   \\?//
 Fa' qualche cosa di cui non sei capace!"  (diceva Henry Miller) ]    (°|°)
[Why use Win$ozz (I say) if ... "even a fool can do that.              )=(
 Do something you aren't good at!" (as Henry Miller used to say) ]

--
Sent via pgsql-novice mailing list (pgsql-novice@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice



--
"Patriotism is the conviction that your country is superior to all others because you were born in it." -- George Bernard Shaw

Re: Escaping literal strings in pg_8.4

От
ennio
Дата:
* Willy-Bas Loos <willybas@gmail.com> [180412, 09:10]:
> well, you have one single quote before the first backslash. so that's the
> end of the quoted string. Then comes a backslash, which is bad syntax. So
> it's the quote, not the backslash that causes the error.
>
> maybe you mean this:
> --
> Vediamo se quest''altro \r\n lo riconosce come un a capo \r\n e questo
> dovrebbe essere il nuovo rigo: ''bla bla bla''!
> --
> psql -c "insert into bibl_memos values(12000, E'`cat prova12000.memo`');"
> INSERT 0 1
> psql -c "select * from bibl_memos"
>   id   |                           val
> -------+---------------------------------------------------------
>  12000 | Vediamo se quest'altro \r                              +
>        |  lo riconosce come un a capo \r                        +
>        |  e questo dovrebbe essere il nuovo rigo: 'bla bla bla'!
> (1 row)
>
> It is kind of hard to get text like that into insert statements.
> I would advise to use COPY. It takes TAB delimeted files, or CSV (e.g. from
> a spreadsheet)
>
> HTH, cheers,
>
> WBL
>
Oh, I realize I was putting the 'E' in the wrong place:

> > Vediamo se quest''altro E'\r\n' lo riconosce come un a capo E'\r\n' e
> > questo dovrebbe essere il nuovo rigo: ''bla bla bla''!

Your suggestion works perfectly!
Thank you Willy and best regards,
     ennio

--
[Perche' usare Win$ozz (dico io) se ..."anche uno sciocco sa farlo.    \\?//
 Fa' qualche cosa di cui non sei capace!"   (diceva Henry Miller) ]    (°|°)
 Ricevo solo messaggi Content-Type: plain/text (no html o multipart).   )=(
 !!! --> e-mail a mio nome via OE (M$) sono false  e infette <-- !!!

Re: Escaping literal strings in pg_8.4

От
Ennio-Sr
Дата:
* Willy-Bas Loos <willybas@gmail.com> [180412, 09:10]:
> well, you have one single quote before the first backslash. so that's the
> end of the quoted string. Then comes a backslash, which is bad syntax. So
> it's the quote, not the backslash that causes the error.
>
> maybe you mean this:
> --
> Vediamo se quest''altro \r\n lo riconosce come un a capo \r\n e questo
> dovrebbe essere il nuovo rigo: ''bla bla bla''!
> --
> psql -c "insert into bibl_memos values(12000, E'`cat prova12000.memo`');"
> INSERT 0 1
> psql -c "select * from bibl_memos"
>   id   |                           val
> -------+---------------------------------------------------------
>  12000 | Vediamo se quest'altro \r                              +
>        |  lo riconosce come un a capo \r                        +
>        |  e questo dovrebbe essere il nuovo rigo: 'bla bla bla'!
> (1 row)
>
> It is kind of hard to get text like that into insert statements.
> I would advise to use COPY. It takes TAB delimeted files, or CSV (e.g. from
> a spreadsheet)
>
> HTH, cheers,
>
> WBL
>
Oh, I realize I was putting the 'E' in the wrong place:

> > Vediamo se quest''altro E'\r\n' lo riconosce come un a capo E'\r\n' e
> > questo dovrebbe essere il nuovo rigo: ''bla bla bla''!

Your suggestion works perfectly!
Thank you Willy and best regards,
     ennio

--
[Perche' usare Win$ozz (dico io) se ..."anche uno sciocco sa farlo.    \\?//
 Fa' qualche cosa di cui non sei capace!"   (diceva Henry Miller) ]    (°|°)
 Ricevo solo messaggi Content-Type: plain/text (no html o multipart).   )=(
 !!! --> e-mail a mio nome via OE (M$) sono false  e infette <-- !!!


Re: Escaping literal strings in pg_8.4

От
Willy-Bas Loos
Дата:
>Oh, I realize I was putting the 'E' in the wrong place:
Well yes, but that was not what caused the error. It was the quote.
If you use COPY (or \copy from psql without superuser rights) you can use TABs to delimit the input. Escaping quotes not necessary, unless your text also contains tabs.

Also, quote_literal(text) can come in handy, but in different scenario's.

cheers,

WBL


--
"Patriotism is the conviction that your country is superior to all others because you were born in it." -- George Bernard Shaw