Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Question about QSqlDatabase::removeDatabase
Forum Updated to NodeBB v4.3 + New Features

Question about QSqlDatabase::removeDatabase

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 604 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Robert Hairgrove
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      R 1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        R 1 Reply Last reply
        2
        • SGaistS SGaist

          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.
          R Offline
          R Offline
          Robert Hairgrove
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            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.

            R Offline
            R Offline
            Robert Hairgrove
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved