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. QSqlDatabase Usage
QtWS25 Last Chance

QSqlDatabase Usage

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 379 Views
  • 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.
  • C Offline
    C Offline
    Crag_Hack
    wrote on last edited by Crag_Hack
    #1

    Some quick questions about QSqlDatabase.

    Should I be doing a QSqlDatabase::removeDatabase() after every modification of the database? Or is a QSqlDatabase::close() appropriate? Or does the QSqlDatabase falling out of scope do the same?

    Do I just do a QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE") once to initialize when needed then a QSqlDatabase::removeDatabase() in the destructor of the program or when everything is complete? Does the connection survive until a QSqlDatabase::removeDatabase() call?

    Do I need to call QSqlDatabase::setDatabaseName(name) each time I modify the database or is just a QSqlDatabase db = QSqlDatabase::database() sufficient?

    See here for some but not all relevant information.

    JonBJ 1 Reply Last reply
    0
    • C Crag_Hack

      @JonB Thanks JonB. When do we close()? At the end of the program / when everything is done with the db?

      @Christian-Ehrlicher modification being INSERT/UPDATE/DELETE etc

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #5

      @Crag_Hack
      Yes, close() when it is that you're done. Either your program has a close down where you shut any open resources, or you could do it just before exiting. Whenever you are done with doing SQL stuff.

      You don't need to do anything after you have done INSERT/UPDATE/DELETEs. setDatabaseName() is a one-off operation. You do it just before open()ing, like you would for a file. https://doc.qt.io/qt-5/qsqldatabase.html#setDatabaseName:

      QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
      db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};FIL={MS Access};DBQ=myaccessfile.mdb");
      if (db.open()) {
          // success!
      }
      
      1 Reply Last reply
      2
      • C Crag_Hack

        Some quick questions about QSqlDatabase.

        Should I be doing a QSqlDatabase::removeDatabase() after every modification of the database? Or is a QSqlDatabase::close() appropriate? Or does the QSqlDatabase falling out of scope do the same?

        Do I just do a QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE") once to initialize when needed then a QSqlDatabase::removeDatabase() in the destructor of the program or when everything is complete? Does the connection survive until a QSqlDatabase::removeDatabase() call?

        Do I need to call QSqlDatabase::setDatabaseName(name) each time I modify the database or is just a QSqlDatabase db = QSqlDatabase::database() sufficient?

        See here for some but not all relevant information.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @Crag_Hack
        I have never used removeDatabase(), just close(). You only need to do QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE") once, and QSqlDatabase::setDatabaseName(name) once.

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

          @Crag_Hack said in QSqlDatabase Usage:

          after every modification of the database?

          what do you mean with 'modification' here?

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

          1 Reply Last reply
          1
          • C Offline
            C Offline
            Crag_Hack
            wrote on last edited by
            #4

            @JonB Thanks JonB. When do we close()? At the end of the program / when everything is done with the db?

            @Christian-Ehrlicher modification being INSERT/UPDATE/DELETE etc

            JonBJ 1 Reply Last reply
            0
            • C Crag_Hack

              @JonB Thanks JonB. When do we close()? At the end of the program / when everything is done with the db?

              @Christian-Ehrlicher modification being INSERT/UPDATE/DELETE etc

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #5

              @Crag_Hack
              Yes, close() when it is that you're done. Either your program has a close down where you shut any open resources, or you could do it just before exiting. Whenever you are done with doing SQL stuff.

              You don't need to do anything after you have done INSERT/UPDATE/DELETEs. setDatabaseName() is a one-off operation. You do it just before open()ing, like you would for a file. https://doc.qt.io/qt-5/qsqldatabase.html#setDatabaseName:

              QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
              db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb, *.accdb)};FIL={MS Access};DBQ=myaccessfile.mdb");
              if (db.open()) {
                  // success!
              }
              
              1 Reply Last reply
              2

              • Login

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