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

QSqlDatabase Usage

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 386 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 13 Aug 2020, 00:16 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.

    J 1 Reply Last reply 13 Aug 2020, 09:05
    0
    • C Crag_Hack
      14 Aug 2020, 00:14

      @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

      J Offline
      J Offline
      JonB
      wrote on 14 Aug 2020, 07:59 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
        13 Aug 2020, 00:16

        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.

        J Offline
        J Offline
        JonB
        wrote on 13 Aug 2020, 09:05 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
        • C Online
          C Online
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 13 Aug 2020, 15:23 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 14 Aug 2020, 00:14 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

            J 1 Reply Last reply 14 Aug 2020, 07:59
            0
            • C Crag_Hack
              14 Aug 2020, 00:14

              @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

              J Offline
              J Offline
              JonB
              wrote on 14 Aug 2020, 07:59 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

              1/5

              13 Aug 2020, 00:16

              • Login

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