[Tutor] pgdb and unicode
Eric Brunson
brunson at brunson.com
Mon Oct 22 19:56:56 CEST 2007
Kent Johnson wrote:
> Ismael Farfán Estrada wrote:
>
>> Hi there.
>> I have a small system in production with wxPython and PostgreSQL running on
>> a machine with Centos 5.
>> At first everytihing was running ok but now a weird bug was discovered:
>> they can't insert characters like á é í ó ú ä ë ñ .... (non english characters)
>> Does anyone knows how can I make pgdb accept that kind of characters?
>> I wrote a little script...
>>
>> But first
>> CREATE DATABASE test;
>> CREATE TABLE tabla(
>> cad character varying
>> ) ;
>>
>> #!/usr/bin/python
>> # -*- coding: utf8 -*-
>> import pgdb
>> con = pgdb.connect(user="farfan", password='000000', host='localhost', database='test')
>> cur = con.cursor()
>> cur.execute("INSERT INTO tabla VALUES ('é')")
>> con.commit()
>> cur.execute(u"INSERT INTO tabla VALUES ('é')")
>> con.commit()
>>
>> As you can see, the first insert statement is executed in ascii format and is succeful,
>> the second, which is in Unicode, fails
>>
>
> Why do you need the whole statement to be Unicode? It looks like the
> database wants utf-8. Probably you should encode the data as utf-8 first.
>
>
>> the problem is thas wxPython will allways send the script in unicode format...
>>
>
> What is coming from wxPython? I guess it is just the data value, not the
> SQL statement or the entire script. Try something like
>
> data= u'é' # Unicode data from wxPython
> data = data.encode('utf-8')
> cur.execute('INSERT INTO tabla VALUES (%s)', [data])
>
Dear Mr. Johnson,
Thank you for doing your part to help eradicate SQL injection attacks. :-)
Sincerely,
The World
(Seriously, though, after the discussion of last week, I always
appreciate seeing good coding practices, even in example and meta code).
> Note I am passing the data as a separate list, not using string
> interpolation. Let the database worry about quoting, etc.
>
> Kent
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list