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. Do SQL connections remain available system-wide from any class after they are opened?
Qt 6.11 is out! See what's new in the release blog

Do SQL connections remain available system-wide from any class after they are opened?

Scheduled Pinned Locked Moved General and Desktop
6 Posts 2 Posters 1.3k Views 1 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.
  • Q Offline
    Q Offline
    qttester5
    wrote on last edited by
    #1

    After opening an Sqlite database, I am wondering why it is possible to run queries in other functions that do not specifically refer to the database you opened. How is it that a query just works? What if you had multiple databases open, how do queries know where to connect to?

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

      Hi,

      The QSqlQuery uses the default database connection by default. If you want to act on another connection, you have the give it as parameter when creating the query.

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

      1 Reply Last reply
      0
      • Q Offline
        Q Offline
        qttester5
        wrote on last edited by
        #3

        So as soon as you create a database connection, it is automatically the "default" database?

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

          If you don't give it a name, it replaces the current default

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

          1 Reply Last reply
          0
          • Q Offline
            Q Offline
            qttester5
            wrote on last edited by
            #5

            I use the setDatabaseName function, which seems to be what is necessary to create an actual file by the name passed to this function, but it must also become the default connection even if I use this function, since other functions are querying correctly without being passed any connection information. Does this sound right?

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

              To create the file yes, but it has nothing to do with the connection handling. The default connection was replaced when you called QSqlDatabase::addDatabase("QSQLITE");

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

              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