[PyQt] managing multiple db connections

Colin McPhail colin.mcphail at talktalk.net
Sat May 29 11:12:49 BST 2010


Hi,

On 28 May 2010, at 23:11, Scott Frankel wrote:

> 
> 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:	
> 
> ...

Have you tried using the two-parameter version of addDatabase(), where the second parameter is the unique name?  I believe that the code above is creating, and then re-creating, the 'default' database.

Regards,
-- CMcP



More information about the PyQt mailing list