Обсуждение: strange characters

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

strange characters

От
"Manera, Villiam"
Дата:

I try the example in the manual:

SQL = "INSERT INTO authors (name) VALUES (%s);"

data = ("O'Reilly", )

cur.mogrify(SQL,data)

"INSERT INTO authors (name) VALUES (E'O''Reilly');"

strange characters:

àE’ß

The same in

 

>>> cur.mogrify("select * from xxx where coll='%(coll)s'",{'coll':'1'});

"select * from xxx where coll='E'1''"

 

Instead with number work fine:

>>> cur.mogrify("select * from xxx where coll='%(coll)s'",{'coll':1});

"select * from xxx where coll='1'"

 

Environment:

Server: PostgreSQL 9.0.1 on x86_64-unknown-linux-gnu, compiled by GCC gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-48), 64-bit         

psycopg2-2.3.1

 

Villiam

 

Re: strange characters

От
Adrian Klaver
Дата:
On Monday 10 January 2011 6:32:24 am Manera, Villiam wrote:
> I try the example in the manual:
>
> SQL = "INSERT INTO authors (name) VALUES (%s);"
>
> data = ("O'Reilly", )
>
> cur.mogrify(SQL,data)
>
> "INSERT INTO authors (name) VALUES (E'O''Reilly');"
>
> strange characters:
>
> àE'ß
>
> The same in
>
> >>> cur.mogrify("select * from xxx where coll='%(coll)s'",{'coll':'1'});
>
> "select * from xxx where coll='E'1''"
>
> Instead with number work fine:
> >>> cur.mogrify("select * from xxx where coll='%(coll)s'",{'coll':1});
>
> "select * from xxx where coll='1'"
>
>
>
> Environment:
>
> Server: PostgreSQL 9.0.1 on x86_64-unknown-linux-gnu, compiled by GCC gcc
> (GCC) 4.1.2 20080704 (Red Hat 4.1.2-48), 64-bit
>
> psycopg2-2.3.1
>
>
>
> Villiam

If you are referring to the 'E' it is not strange. That is the new Postgres
escape identifier. The backslash escape is being phased out.

--
Adrian Klaver
adrian.klaver@gmail.com

Re: strange characters

От
Adrian Klaver
Дата:
On Monday 10 January 2011 6:32:24 am Manera, Villiam wrote:
> I try the example in the manual:
>
> SQL = "INSERT INTO authors (name) VALUES (%s);"
>
> data = ("O'Reilly", )
>
> cur.mogrify(SQL,data)
>
> "INSERT INTO authors (name) VALUES (E'O''Reilly');"
>
> strange characters:
>
> àE'ß
>
> The same in
>
> >>> cur.mogrify("select * from xxx where coll='%(coll)s'",{'coll':'1'});
>
> "select * from xxx where coll='E'1''"
>
> Instead with number work fine:
> >>> cur.mogrify("select * from xxx where coll='%(coll)s'",{'coll':1});
>
> "select * from xxx where coll='1'"


Forgot to add the link to the docs in my previous post:

http://www.postgresql.org/docs/9.0/interactive/sql-syntax-lexical.html
See section 4.1.2.2. String Constants with C-Style Escapes


--
Adrian Klaver
adrian.klaver@gmail.com

R: strange characters

От
"Manera, Villiam"
Дата:
Adrian wrote:
>If you are referring to the 'E' it is not strange. That is the new
Postgres
>escape identifier. The backslash escape is being phased out.

Ok, but the execute cursor will abort:
>>> cur.execute("select * from anamat.collezioni where
coll='%(coll)s'",{'coll':'1'});
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
ProgrammingError: syntax error at or near "1"
LINE 1: select * from anamat.collezioni where coll='E'1''


Villiam

Re: R: strange characters

От
Adrian Klaver
Дата:
On Monday 10 January 2011 7:01:29 am Manera, Villiam wrote:
> Adrian wrote:
> >If you are referring to the 'E' it is not strange. That is the new
>
> Postgres
>
> >escape identifier. The backslash escape is being phased out.
>
> Ok, but the execute cursor will abort:
> >>> cur.execute("select * from anamat.collezioni where
>
> coll='%(coll)s'",{'coll':'1'});
> Traceback (most recent call last):
>   File "<interactive input>", line 1, in <module>
> ProgrammingError: syntax error at or near "1"
> LINE 1: select * from anamat.collezioni where coll='E'1''
>
>
> Villiam

Well it should be E'1' not 'E'1''. This is because you are doing this '%(coll)s'
instead of this %(coll)s. In other words you are quoting the passed parameter.
In any case if coll is an integer column just pass an integer.

--
Adrian Klaver
adrian.klaver@gmail.com