pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Using escape strings in an insert statement.
6+ messages / 3 participants
[nested] [flat]

* Using escape strings in an insert statement.
@ 2007-07-02 22:45  Paul Lambert <paul.lambert@autoledgers.com.au>
  0 siblings, 1 reply; 6+ messages in thread

From: Paul Lambert @ 2007-07-02 22:45 UTC (permalink / raw)
  To: pgsql-sql

I'm having a little glitch in using escape strings within strings.

The following will best explain my issue:

tester=# create table testing (test_text text);
CREATE TABLE

tester=# insert into testing (test_text) values ('abc\\123');
WARNING:  nonstandard use of \\ in a string literal
LINE 1: insert into testing (test_text) values ('abc\\123');
                                                 ^
HINT:  Use the escape string syntax for backslashes, e.g., E'\\'.
INSERT 0 1

^^^ This works, but I still get an error/warning telling me to use E'\\' 
  - which I do:

tester=# insert into testing (test_text) values ('abcE'\\'123');
Invalid command \. Try \? for help.
tester(#

^^^ And this just doesn't work at all.


The help itself (ch 4.1.2.1) tells me to use double backslash "Thus, to 
include a backslash character, write two backslashes (\\). "

Which one is the correct syntax and how can I make it not return 
anything other than a successful insert?

Noob question, I know...

But TIA.

(Version is 8.2.3 on Weenblowz if that is of any relevance)

--
Paul Lambert
Database Administrator
AutoLedgers



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Using escape strings in an insert statement.
@ 2007-07-02 23:11  Michael Glaesemann <grzm@seespotcode.net>
  parent: Paul Lambert <paul.lambert@autoledgers.com.au>
  0 siblings, 2 replies; 6+ messages in thread

From: Michael Glaesemann @ 2007-07-02 23:11 UTC (permalink / raw)
  To: Paul Lambert <paul.lambert@autoledgers.com.au>; +Cc: pgsql-sql


On Jul 2, 2007, at 17:45 , Paul Lambert wrote:

> tester=# insert into testing (test_text) values ('abcE'\\'123');

This should be
INSERT INTO testing (test_text) values (E'abc\123');

> The help itself (ch 4.1.2.1) tells me to use double backslash  
> "Thus, to include a backslash character, write two backslashes (\\). "

Note that the String Constants section (4.1.2.1) says put the E  
"before the opening single quote".

http://www.postgresql.org/docs/8.2/interactive/sql-syntax- 
lexical.html#SQL-SYNTAX-CONSTANTS

> An escape string constant is specified by writing the letter E  
> (upper or lower case) just before the opening single quote, e.g.  
> E'foo'.

Also be sure to read the Caution section.

Using \ as an escape character is the old non-standard PostgreSQL  
escape syntax that the WARNING (above) is, uh, warning you about.  
With standard_conforming_strings on (i.e., follow the SQL spec), the  
backslash is just a backslash character.

> Which one is the correct syntax and how can I make it not return  
> anything other than a successful insert?

Depends on the setting of standard_conforming_strings.

Michael Glaesemann
grzm seespotcode net





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Using escape strings in an insert statement.
@ 2007-07-02 23:26  Paul Lambert <paul.lambert@autoledgers.com.au>
  parent: Michael Glaesemann <grzm@seespotcode.net>
  1 sibling, 1 reply; 6+ messages in thread

From: Paul Lambert @ 2007-07-02 23:26 UTC (permalink / raw)
  To: pgsql-sql

Michael Glaesemann wrote:
> 
> On Jul 2, 2007, at 17:45 , Paul Lambert wrote:
> 
>> tester=# insert into testing (test_text) values ('abcE'\\'123');
> 
> This should be
> INSERT INTO testing (test_text) values (E'abc\123');
> 
>> The help itself (ch 4.1.2.1) tells me to use double backslash "Thus, 
>> to include a backslash character, write two backslashes (\\). "
> 
> Note that the String Constants section (4.1.2.1) says put the E "before 
> the opening single quote".
> 
> http://www.postgresql.org/docs/8.2/interactive/sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS 
> 
> 

Missed that part - my apologies. Time to read the manuals more 
thoroughly it would seem.

>> An escape string constant is specified by writing the letter E (upper 
>> or lower case) just before the opening single quote, e.g. E'foo'.
> 
> Also be sure to read the Caution section.
> 
> Using \ as an escape character is the old non-standard PostgreSQL escape 
> syntax that the WARNING (above) is, uh, warning you about. With 
> standard_conforming_strings on (i.e., follow the SQL spec), the 
> backslash is just a backslash character.
> 
>> Which one is the correct syntax and how can I make it not return 
>> anything other than a successful insert?
> 
> Depends on the setting of standard_conforming_strings.
> 
> Michael Glaesemann
> grzm seespotcode net
> 

Looks like it's a toss-up between turning standard_conforming_strings on 
or turning escape_string_warning off, both seem to have the same effect 
in not giving the error anymore.

I'll go with your suggestion though, I guess since it's a new install 
not a previous upgrade of an old it's technically more correct to 
conform to current standards rather than attempt to conform to old behavior.

Cheers for the help - much appreciated.

-- 
Paul Lambert
Database Administrator
AutoLedgers




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Using escape strings in an insert statement.
@ 2007-07-03 00:13  Michael Glaesemann <grzm@seespotcode.net>
  parent: Paul Lambert <paul.lambert@autoledgers.com.au>
  0 siblings, 0 replies; 6+ messages in thread

From: Michael Glaesemann @ 2007-07-03 00:13 UTC (permalink / raw)
  To: Paul Lambert <paul.lambert@autoledgers.com.au>; +Cc: pgsql-sql


On Jul 2, 2007, at 18:26 , Paul Lambert wrote:

> Looks like it's a toss-up between turning  
> standard_conforming_strings on or turning escape_string_warning  
> off, both seem to have the same effect in not giving the error  
> anymore.

A warning is not an error :)

Michael Glaesemann
grzm seespotcode net





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Using escape strings in an insert statement.
@ 2007-07-03 15:49  Erik Jones <erik@myemma.com>
  parent: Michael Glaesemann <grzm@seespotcode.net>
  1 sibling, 1 reply; 6+ messages in thread

From: Erik Jones @ 2007-07-03 15:49 UTC (permalink / raw)
  To: Michael Glaesemann <grzm@seespotcode.net>; +Cc: Paul Lambert <paul.lambert@autoledgers.com.au>; pgsql-sql


On Jul 2, 2007, at 6:11 PM, Michael Glaesemann wrote:

>
> On Jul 2, 2007, at 17:45 , Paul Lambert wrote:
>
>> tester=# insert into testing (test_text) values ('abcE'\\'123');
>
> This should be
> INSERT INTO testing (test_text) values (E'abc\123');

No, that will leave him with the string 'abc23' beinginserted, he  
wants the backslash to be included in the string, that's why he had  
two, so it should be:

INSERT INTO testing (test_text) values (E'abc\\123');

>
>> The help itself (ch 4.1.2.1) tells me to use double backslash  
>> "Thus, to include a backslash character, write two backslashes (\ 
>> \). "
>
> Note that the String Constants section (4.1.2.1) says put the E  
> "before the opening single quote".
>
> http://www.postgresql.org/docs/8.2/interactive/sql-syntax- 
> lexical.html#SQL-SYNTAX-CONSTANTS
>
>> An escape string constant is specified by writing the letter E  
>> (upper or lower case) just before the opening single quote, e.g.  
>> E'foo'.
>
> Also be sure to read the Caution section.
>
> Using \ as an escape character is the old non-standard PostgreSQL  
> escape syntax that the WARNING (above) is, uh, warning you about.  
> With standard_conforming_strings on (i.e., follow the SQL spec),  
> the backslash is just a backslash character.
>
>> Which one is the correct syntax and how can I make it not return  
>> anything other than a successful insert?
>
> Depends on the setting of standard_conforming_strings.

With standard_conforming_strings turned on, it would just need to be:

INSERT INTO test (test_text) values ('abc\123');

>
> Michael Glaesemann
> grzm seespotcode net
>
>
>
> ---------------------------(end of  
> broadcast)---------------------------
> TIP 7: You can help support the PostgreSQL project by donating at
>
>                http://www.postgresql.org/about/donate

Erik Jones

Software Developer | Emma®
erik@myemma.com
800.595.4401 or 615.292.5888
615.292.0777 (fax)

Emma helps organizations everywhere communicate & market in style.
Visit us online at http://www.myemma.com





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Using escape strings in an insert statement.
@ 2007-07-03 17:56  Michael Glaesemann <grzm@seespotcode.net>
  parent: Erik Jones <erik@myemma.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Michael Glaesemann @ 2007-07-03 17:56 UTC (permalink / raw)
  To: Erik Jones <erik@myemma.com>; +Cc: Paul Lambert <paul.lambert@autoledgers.com.au>; pgsql-sql


On Jul 3, 2007, at 10:49 , Erik Jones wrote:

> On Jul 2, 2007, at 6:11 PM, Michael Glaesemann wrote:
>
>>
>> On Jul 2, 2007, at 17:45 , Paul Lambert wrote:
>>
>>> tester=# insert into testing (test_text) values ('abcE'\\'123');
>>
>> This should be
>> INSERT INTO testing (test_text) values (E'abc\123');
>
> No, that will leave him with the string 'abc23' beinginserted, he  
> wants the backslash to be included in the string, that's why he had  
> two, so it should be:
>
> INSERT INTO testing (test_text) values (E'abc\\123');

Ah, right. Thanks for the correction, Erik.

Michael Glaesemann
grzm seespotcode net





^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2007-07-03 17:56 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-07-02 22:45 Using escape strings in an insert statement. Paul Lambert <paul.lambert@autoledgers.com.au>
2007-07-02 23:11 ` Michael Glaesemann <grzm@seespotcode.net>
2007-07-02 23:26   ` Paul Lambert <paul.lambert@autoledgers.com.au>
2007-07-03 00:13     ` Michael Glaesemann <grzm@seespotcode.net>
2007-07-03 15:49   ` Erik Jones <erik@myemma.com>
2007-07-03 17:56     ` Michael Glaesemann <grzm@seespotcode.net>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox