[Tutor] pgdb and unicode

Ismael Farfán Estrada sulfurfff at hotmail.com
Mon Oct 22 19:44:18 CEST 2007


>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])

Tanks, that did the trick:
data = []
data.append(self.marca.GetLineText(0))#.encode('utf-8'))
sql = "INSERT INTO Marca (marca) VALUES (%s);"
cur.execute(sql, data)

>
>Note I am passing the data as a separate list, not using string 
>interpolation. Let the database worry about quoting, etc.
>
>Kent

It seems that just by sending the data in a list fixed the problem
though adding the encode doesn't harm either ...
I'll have to make a looot of changes.

by the way, does sending the data as a list prevent SQL injection?
I haven't worried for that yet.

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE!
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/


More information about the Tutor mailing list