[PyQt] How to communicate with PostgreSQL?

P. Mathé pmathe at neuf.fr
Sun Jan 27 18:42:05 GMT 2008


Le dimanche 27 janvier 2008, nishith datta a écrit :
> hi all,
> I also have a similar problem, but only with Postgre SQL.
> The db.open() call returns a false and the error says data base driver is not loaded. Can someone advise me how to build postgre support into pyqt.
> I am using pyqt 4.3 on winXP. I also have qt4.3.3 which is open source and compiled with mingw.
> 
> thanks 
> nishith
> 
> 
>        
> ---------------------------------
> Never miss a thing.   Make Yahoo your homepage.

I found pyPgSQL much more "pythonic", this is what I use, as below :

from pyPgSQL import PgSQL as pg

dbName= your_data_base_name

def dbConnect(cClient='utf8'):
    try:
        param='::%s' % dbName
        codageClient=str(cClient).replace('_','').replace('-','')  # codageClient must be type str
        if codageClient=='utf8' : cx=pg.connect(param,client_encoding=codageClient,unicode_results=True)
        else : cx=pg.connect(param,client_encoding=codageClient,unicode_results=False)
        curseur=cx.cursor()
        curseur.execute("set client_encoding to %s" % codageClient)
        curseur.execute("show client_encoding")
        codageClient=curseur.fetchone()[0]
        return [cx,curseur,dbName,codageClient]
    except:
        return [None,None,None,None]
........
self.cx,self.curseur,self.dbname,self.codageClient=common.dbConnect('utf8')



More information about the PyQt mailing list