Re: R: strange characters

Lists: psycopg
From: "Manera, Villiam" <vmanera(at)manord(dot)com>
To: <psycopg(at)postgresql(dot)org>
Subject: strange characters
Date: 2011-01-10 14:32:24
Message-ID: 4B0D10F422F80F479C5372363DE21FB1026A2E36@mail2.manord.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: psycopg

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


From: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>
To: psycopg(at)postgresql(dot)org
Cc: "Manera, Villiam" <vmanera(at)manord(dot)com>
Subject: Re: strange characters
Date: 2011-01-10 14:47:06
Message-ID: 201101100647.07021.adrian.klaver@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: psycopg

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(dot)klaver(at)gmail(dot)com


From: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>
To: psycopg(at)postgresql(dot)org
Cc: "Manera, Villiam" <vmanera(at)manord(dot)com>
Subject: Re: strange characters
Date: 2011-01-10 15:00:08
Message-ID: 201101100700.09096.adrian.klaver@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: psycopg

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(dot)klaver(at)gmail(dot)com


From: "Manera, Villiam" <vmanera(at)manord(dot)com>
To: <psycopg(at)postgresql(dot)org>
Cc: "Adrian Klaver" <adrian(dot)klaver(at)gmail(dot)com>
Subject: R: strange characters
Date: 2011-01-10 15:01:29
Message-ID: 4B0D10F422F80F479C5372363DE21FB1026A2E4D@mail2.manord.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: psycopg

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


From: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>
To: "Manera, Villiam" <vmanera(at)manord(dot)com>
Cc: psycopg(at)postgresql(dot)org
Subject: Re: R: strange characters
Date: 2011-01-10 15:24:33
Message-ID: 201101100724.33395.adrian.klaver@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: psycopg

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(dot)klaver(at)gmail(dot)com