[PyQt] Error using QSqlDatabase.removeDatabase()

Sibylle Koczian nulla.epistola at web.de
Sun Feb 16 09:38:09 GMT 2014


Am 15.02.2014 21:39, schrieb michael h:
>
> 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.
>

Thank you. You're right, I didn't know how to translate the C++ code to 
Python. And the PySide documentation isn't helpful in this case:

"Example:

# WRONG
db = QSqlDatabase.database("sales")
query = QSqlQuery("SELECT NAME, DOB FROM EMPLOYEES", db)
QSqlDatabase.removeDatabase("sales") # will output a warning

# "db" is now a dangling invalid database connection,
# "query" contains an invalid result set

The correct way to do it:

db = QSqlDatabase.database("sales")
query = QSqlQuery("SELECT NAME, DOB FROM EMPLOYEES", db)
# Both "db" and "query" are destroyed because they are out of scope
QSqlDatabase.removeDatabase("sales") # correct
"

No difference at all, db and query _aren't_ destroyed. Didn't think of 
"del", which isn't used very often in Python as far as I can see.

Greetings
Sibylle


More information about the PyQt mailing list