[PyQt] How to close QSqlDatabase connection?

Sibylle Koczian Sibylle.Koczian at t-online.de
Mon Nov 12 14:20:41 GMT 2007


Am Sonntag, 11. November 2007 16:09:04 schrieb Hans-Peter Jansen:
> Dear Sibylle,
>
> I can only talk for PyQt3, through (yes, I'm retro..), but..
>

As far as I can see the version differences are particularly big in this area, 
because of the model/view design pattern. No QDataTable to start with. And  
I've not used PyQt3 beyond a little playing around with the tutorial.

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

At the moment I'm inclined to search the error in my "main" function. I've 
taken the "cachedtable.py" example and changed it to use my SQLite database. 
This example had no "close" code at all, and when I put a "closeEvent" method 
in, I got the error message if and only if this method contained a call 
to "removeDatabase". But removing this line from my own program didn't change 
anything. 

So I looked around and saw that I had this:

def main(args):
    app = QtGui.QApplication(args)
    if not visa_conn():
        sys.exit(1)
    visa_mf = VisaMF()
    visa_mf.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main(sys.argv[1:])

while "cachedtable.py" has no separate main function and calls the app 
constructor with the full list sys.argv. Doing the same in my own example 
seems to do the trick. But why?

Thank you,
Sibylle

-- 
Dr. Sibylle Koczian


More information about the PyQt mailing list