[PyQt] Error using QSqlDatabase.removeDatabase()

michael h michaelkenth at gmail.com
Sat Feb 15 20:39:47 GMT 2014


On Sat, Feb 15, 2014 at 1:42 PM, Sibylle Koczian <nulla.epistola at web.de>wrote:

> Hello,
>
> I don't understand the error I get with the following script:
>
> #!/usr/bin/env python
>
> import sys
> from PyQt5 import QtCore
> from PyQt5 import QtSql
>
> def checkQSQLITE():
>     result = QtSql.QSqlDatabase.isDriverAvailable('QSQLITE')
>     print("Treiber verfügbar? {}".format(result))
>     db = QtSql.QSqlDatabase.addDatabase("QSQLITE")
>     cname = db.connectionName()
>     db.setDatabaseName(":memory:")
>     result = db.open()
>     print("Datenbank geöffnet? {}".format(result))
>     if result:
>         db.close()
>         if db.isOpen():
>             print("Datenbank konnte nicht geschlossen werden.")
>             print(db.lastError().text())
>         else:
>             print("Datenbank geschlossen.")
>     QtSql.QSqlDatabase.removeDatabase(cname)
>
> def main(args):
>     app = QtCore.QCoreApplication(args)
>     checkQSQLITE()
>
> if __name__ == "__main__":
>     main(sys.argv)
>
>
> Result:
>
> Treiber verfügbar? True
> Datenbank geöffnet? True
> Datenbank geschlossen.
> QSqlDatabasePrivate::removeDatabase: connection
> 'qt_sql_default_connection' is still in use, all queries will cease to work.
>
> Why is the connection still in use after it's been closed? What should I
> do to put it out of use? There is no open query, because no query at all
> has been executed.
>
> This problem isn't new, but I found only questions (my own old one among
> them), no answer, at least not for PyQt.
>
> Thank you for help,
> Sibylle
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt


The docs for removeDatabase mention the potential issue:

The correct way to do it:

{
    QSqlDatabase db = QSqlDatabase::database("sales");
    QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
}
// Both "db" and "query" are destroyed because they are out of scope
QSqlDatabase::removeDatabase("sales"); // correct

How this relates to python may not be obvious if you are not familiar with
c++. When I added a "del db" (before the removeDatabase call) so that
QSqlDatabase is collected/destroyed, the error no longer occurred.

- Michael
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20140215/9ee45a21/attachment.html>


More information about the PyQt mailing list