[PyQt] How to close QSqlDatabase connection?

Hans-Peter Jansen hpj at urpla.net
Sun Nov 11 15:09:04 GMT 2007


Dear Sibylle,

I can only talk for PyQt3, through (yes, I'm retro..), but..

Am Sonntag, 11. November 2007 schrieb Sibylle Koczian:
> Hello,
>
> I'm trying to write a very small database application, but I can't get
> rid of this error message when I close the main
> window: "QSqlDatabasePrivate::removeDatabase:
> connection 'qt_sql_default_connection' is still in use, all queries will
> cease to work."
>
> At the moment I'm using this code to open and to close the database:
>
> Global function of the module:
> def visa_conn():
>     db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
>     db.setDatabaseName("/home/sib/pfotweg/visa_s.db")
>     return db.open()
>
> Method of the main window, connected with a button:
>     def close(self):

My close connection code typically contains code like (for a QDataTable 
derived case in this example):

        if self.sqlCursor():
            self.setSqlCursor()

You may want to try to give the db instance an explicit name, and use that 
on tear down...

>         db = QtSql.QSqlDatabase.database()
>         db.close()
>         db.removeDatabase(db.connectionNames()[0])
>         QtGui.QMainWindow.close(self)

Hth,
  Pete


More information about the PyQt mailing list