[Tutor] pgdb and unicode

Kent Johnson kent37 at tds.net
Mon Oct 22 18:58:21 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
>   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])

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


More information about the Tutor mailing list