Question about QSqlDatabase::removeDatabase
-
Working with Qt's SQLite driver ... when switching database files, can I just call the
close()
member function before callingsetDatabaseName()
andopen()
for the new database file, i.e. reusing the same named connection, or do I have to callQSqlDatabase::removeDatabase()
first and then callQSqlDatabase::addDatabase()
again?At least for SQLite, it seems to work OK if I never call
removeDatabase()
at all. But maybe there is a leak somewhere? The docs are very clear about not having any active database connections or queries before calling this; however, I'm not sure that it is necessary. -
Hi,
AFAIK, there's nothing wrong with that. Having several database connections is usually when:
- Connecting to several different databases
- Connecting to the same database from different threads.
-
If you've an active QSqlQuery somewhere and then calling setDatabaseName() you will likely get in trouble since there is no guarantee that this has to work. It maybe works for QSQlite.
-
@Christian-Ehrlicher Thank you. Since each QSqlQuery object and each QSqlDatabase object only exist for the life of one function body, at most, this should not be a problem.