|
From: Gerhard <ger...@gm...> - 2001-11-20 08:11:40
|
On Mon, Nov 19, 2001 at 07:26:49PM -0800, Mark McEahern wrote:
> Hi, I'm trying to insert unicode strings into PostgreSQL via PgSQL and I'm
> having a devil of a time with it. Here's what I tried:
>
> $ createdb -E UNICODE testu
> $ psql testu
> # \encoding
> UNICODE
> # create table person (firstname text)
> CREATE
Mark,
there might be an (ugly) workaround for the current version of pyPgSQL. A lot
of functionality is lost, but at least it seems to allow using Unicode
databases:
from pyPgSQL import PgSQL
# with CREATE TABLE GH(NAME VARCHAR(20));
db = PgSQL.connect(database="testu")
cursor = db.cursor()
name = unicode("Bärwurz", "latin-1")
stmnt = u"insert into gh (name) values ('%s')" % name
stmnt = stmnt.encode("utf-8")
cursor.execute(stmnt)
cursor.execute("select * from gh")
print unicode(cursor.fetchone()[0], "utf-8").encode("latin-1")
The reason for Unicode currently not working right is that almost all of the
pyPgSQL code excepts a StringType and won't work with UnicodeType parameters.
But I fear that's only part of the problem. *Really* fixing it will probably be
hard work. We'll also have to decide what the desired behaviour is in such
cases. I think the usage of Unicode will also have to depend on the setting of
the current client encoding, for example.
Btw. the only other db module I could find that does *handle* Unicode at all is
mxODBC.
Gerhard
--
mail: gerhard <at> bigfoot <dot> de registered Linux user #64239
web: http://www.cs.fhm.edu/~ifw00065/ OpenPGP public key id 86AB43C0
public key fingerprint: DEC1 1D02 5743 1159 CD20 A4B6 7B22 6575 86AB 43C0
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))
|