[PyQt] managing multiple db connections

Scott Frankel frankel at circlesfx.com
Fri May 28 23:11:52 BST 2010


Hi all,

What's the best way to manage multiple database connections in an  
application?  Specifically, how can I manage which db connection is  
used for models, queries, &c.?


The documentation says that multiple connections can be declared with  
a unique name for each on calling addDatabase().  My simple  
implementation yields errors and doesn't seem to provide a  means to  
load data or call queries on one or the other.  For example,

	db2 = QtSql.QSqlDatabase.addDatabase("QPSQL")
	db2.setDatabaseName("foo_db")

	db1 = QtSql.QSqlDatabase.addDatabase("QSQLITE")
	db1.setDatabaseName(":memory:")

This yields the following errors:	

QSqlDatabasePrivate::removeDatabase: connection  
'qt_sql_default_connection' is still in use, all queries will cease to  
work.
QSqlDatabasePrivate::addDatabase: duplicate connection name  
'qt_sql_default_connection', old connection removed.

Then creating and setting a data model uses which database?  Both?

	self.theModel = QtSql.QSqlTableModel(self)
	self.theModel.setTable("color")


See the main() method in the attached example.  Thanks in advance!
Scott


-------------- next part --------------
A non-text attachment was scrubbed...
Name: dbTest.py
Type: text/x-python-script
Size: 3696 bytes
Desc: not available
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20100528/a8f24eda/attachment.bin>
-------------- next part --------------







More information about the PyQt mailing list