logo       

Re: Coding when open conection: msg#00018

python.db.psycopg.devel

Subject: Re: Coding when open conection

Oswaldo Hernández wrote:
Hi all,

I'm new in this list. My name is Oswaldo and my english is bad (sorry).

I have a problem when I try to connect with a bad user or a bad database name:

Server:
Postgres 8.1 windowsXP
Database encoding: UTF8
postgres.conf
client_encoding UTF8
lc_messages es_ES

Client:
Python 2.4.2 + psycopg2 2.0b8
defaultecoding UTF8
Windows XP

The Conection:

import psycopg2 as pg
import psycopg2.extras as pgxtra
....
pgcon = pg.connect(" ..... ")
pgcon.set_client_encoding('UTF8')


When I connect with a valid user and database there is no problem with spanish chars and simbols.

But if I put a bad user in the connection string I have the following message:

File "C:\Python24\lib\encodings\utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xab in position 96: unexpected code byte

These exception is caused by the Postgres spanish error message:
FATAL: no existe el rol «user»

where 'user' is eclosed between '«' and '»' chars.

I try to connect with a bad user with PgAdminIII and EMS and both show the message correctly.
Hereby I suppose that the problem is that psycopg2 don't use utt8 until set_client_encoding is sended, and the message is received before it.

Thanks.


Why don't you use exceptions ?? :



import sys
import psycopg


try:
con = psycopg.connect("dbname=leonel user=leonel")
except StandardError, error:
print "no se conecto"
sys.exit(2)

cur=con.cursor()
cur.execute("select * from test")
con.commit()
con.close()




If there's an error will stop printing "no se conecto"




Leonel


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise