Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Unicode -> Python -> DBAPI -> PyPgSQL -> PostgreSQL

Reply
Thread Tools

Unicode -> Python -> DBAPI -> PyPgSQL -> PostgreSQL

 
 
Rene Pijlman
Guest
Posts: n/a
 
      11-03-2003
I can't seem to find any way to specify the character encoding with the DB
API implementation of PyPgSQL. There is no mention of encoding and Unicode
in the DB API v2.0 spec and the PyPgSQL README.

When I have Unicode strings in Python and store it in a PostgreSQL Unicode
database, will the data automatically be correctly encoded? Or do I need
to specify the UTF-8 client encoding on the database connection somehow?

I'm using the current packages of Debian stable (woody):
Python 2.2
PyPgSQL 2.0
PostgreSQL 7.2 (database created with UNICODE / UTF-8 encoding)

--
René Pijlman
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?Gerhard_H=E4ring?=
Guest
Posts: n/a
 
      11-03-2003
Rene Pijlman wrote:
> I can't seem to find any way to specify the character encoding with the DB
> API implementation of PyPgSQL. There is no mention of encoding and Unicode
> in the DB API v2.0 spec and the PyPgSQL README. [...]


See section 2.2.5 in the pyPgSQL README:

pyPgSQL has a few extensions that make it possible to insert Unicode strings
into PostgreSQL and fetch unicode strings instead of byte strings from the
database.

The module-level connect() function has two Unicode-related parameters:

- client_encoding
- unicode_results

*client_encoding* accepts the same parameters as the encode method
of Unicode strings. If you also want to set a policy for encoding
errors, set client_encoding to a tuple, like ("koi8-r", "replace")

Note that you still must make sure that the PostgreSQL client is
using the same encoding as set with the client_encoding parameter.
This is typically done by issuing a "SET CLIENT_ENCODING TO ..."
SQL statement immediately after creating the connection.

If you also want to fetch Unicode strings from the database, set
*unicode_results* to 1.

For example, assuming a database created with *createdb mydb -E UNICODE*
and a
table *TEST(V VARCHAR(50))*:

>>> from pyPgSQL import PgSQL
>>> cx = PgSQL.connect(database="mydb", client_encoding="utf-8",

unicode_results=1)
>>> cu = cx.cursor()
>>> cu.execute("set client_encoding to unicode")
>>> cu.execute("insert into test(v) values (%s)", (u'\x99sterreich',))
>>> cu.execute("select v from test")
>>> cu.fetchone()

[u'\x99sterreich']
>>>



-- Gerhard


 
Reply With Quote
 
 
 
 
Rene Pijlman
Guest
Posts: n/a
 
      11-03-2003
Gerhard Häring:
>Rene Pijlman:
>> I can't seem to find any way to specify the character encoding with the DB
>> API implementation of PyPgSQL. There is no mention of encoding and Unicode
>> in the DB API v2.0 spec and the PyPgSQL README. [...]

>
>See section 2.2.5 in the pyPgSQL README:


Well, its not in this README I found in the Debian package python2.2-pgsql
with pyPgSQL 2.0:
#ident "@(#) $Id: README,v 1.20 2001/11/05 01:18:12 ghaering Exp $"
pyPgSQL - v2.0: Python DB-API 2.0 Compliant Interface Module for
PostgreSQL.

But this tells me I probably need to upgrade to pyPgSQL 2.3 or 2.4:
"Q: I’ve heard of Unicode support for pyPgSQL. What’s the current status?
A: It’s integrated in pyPgSQL 2.3."
http://pypgsql.sourceforge.net/pypgsql-faq.pdf

Thanks a lot Gerhard, you've put me on the right track.

--
René Pijlman
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
install pyPgSQL on Windows for python 2.5 someone Python 3 05-12-2009 06:48 AM
DBApi Question with MySQL Hans Müller Python 1 12-12-2007 05:43 PM
DBAPI Loss DB Connection Greg Copeland Python 0 04-02-2007 07:30 PM
using array as execute parameter in dbapi gglegrp112@alhashash.net Python 2 07-22-2006 09:23 AM
DBAPI Paramstyle Bob Parnes Python 6 03-28-2005 09:43 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57