[PyQt] postgresql

Nahuel Defossé nahueldefosse at yahoo.com.ar
Thu Mar 19 03:01:53 GMT 2009


Stuart:

> I was kind of hoping that PyQt's Qsql classes could save me some
> work, but AFAICT, I need a Qsql.Database object to use them and
> but a Postgreql driver is not available in binary for Windows.
You have to compile Qt and PyQt with Postgre suport on windows. I had to do 
this for MySQL support, and I'll try to translate it to Posgres.

You need to download 
0) Postgres, I assume you've already downloaded this and installed it.
1) Grab Qt 4.4.3 from 
ftp://ftp.trolltech.com/qt/source/qt-win-opensource-4.4.3-mingw.exe
That will install Qt and MinGW (the compiler needed for it). After installing 
make sure to include C:\mingw\bin (or your install location) in your PATH.

You'll have to compile Qt before compiling PyQt with posgres support, in order 
to get Qt do this:
configure.exe -qt-sql-psql -I C:/PostgreSQL/include -L C:/PostgreSQL/lib -l pq
You might want to check if Qt's configure.exe extra parameteres such as images 
format support.
Then run:
mingw32-make
mingw32-make install
These steps might take some time, around 2hs in a Core2 Duo 1.9GHz with 1Gb or 
RAM. You could reduce that amount of time by disabling unnecessary things.

One Qt is compillled and installed, I'd sugget to check if Qt driver is 
present. Make a small project, with something like this (I assume you have 
little C++ experience, but you could ask at #qt in freenode, it's a very 
active channel):
qDebug( QSqlDatabase::drivers().join(", ") )
Check if postgres is listed, take into account that you will need a DLL file 
in the very same folder where the binary is poduced, otherwise even the 
driver might have been compiled, it won't load.

2) http://www.riverbankcomputing.co.uk/static/Downloads/sip4/sip-4.7.9.zip
If mingw's bin folder is in the PATH you won't find any problem in compiling 
sip. Compile sip will be simpler and faster than Qt, just make sure it's in 
your path after install. Steps are configure.py, mingw32-make and 
mingw32-make install

3) Finally it's PyQt4's turn. configure.py, mingw32-make, mingw32-make install 
and you're done. Copy postgres DLLs to PyQt4 dll folder, and check if it's 
loaded with QtSql.QSqlDatabase.drivers(). 

> So an alternative would be to reimplement the Qsql classes using
> based on the Python DBAPI protocol, but maybe the cost/benefit ratio
> for that is too high?  
I'd say far to high. Qt has it own philosophy with databases, if you want 
snappy grids, and automatic updates and all that stuff, i'd suggest to stick 
to PyQt4 cplusplus-ish way of dealing with data. It's not pythoninc but it 
works quite well. I must admit ORM's are much more nice to work with, but SQL 
is not so bad. 

In my little experience with this (less than 6 months):
I had a small codebase in SQLAlchemy ant Twisted before we decided to switch 
from a web application to PyQt4. SQLAlchemy is definitely easier to write 
than SQL, so I tried to code as much as SA code as I could, and when it was 
not possible I used QtSql code. 

> (Or maybe someone has already done it?) 
When Qt code was too ugly, I wrapped it with python. For example I wrote this 
class
http://bitbucket.org/D3f0/dsem/src/tip/pyscada/gui/qt_dbhelp.py Line 106

I know that pythonizing Qt's QtSql could go much deeper, and it would be a 
nice project, but, I have a schedule with this project and what I've done is 
enough.

I hope this might have brought some light into your search.


Regards
Nahuel


More information about the PyQt mailing list