Qt World Summit: Submit your Presentation

Question about QSqlDatabase::removeDatabase

  • Working with Qt's SQLite driver ... when switching database files, can I just call the close() member function before calling setDatabaseName() and open() for the new database file, i.e. reusing the same named connection, or do I have to call QSqlDatabase::removeDatabase() first and then call QSqlDatabase::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.

  • Lifetime Qt Champion


    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.

  • Lifetime Qt Champion

    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.

  • @SGaist Thank you. I will have only two different named connections, but not at the same time: One connection to an existing database file, and another to a ":memory:" database. This should not be a problem.

  • @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.

Log in to reply