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 with MYSQL management in the app
Forum Updated to NodeBB v4.3 + New Features

QSqlDatabase with MYSQL management in the app

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 4 Posters 5.4k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #13

    removeDatabase is a static method.

    Take a look at the its documentation to see how to avoid having that warning message.

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

    Cobra91151C 1 Reply Last reply
    1
    • SGaistS SGaist

      removeDatabase is a static method.

      Take a look at the its documentation to see how to avoid having that warning message.

      Cobra91151C Offline
      Cobra91151C Offline
      Cobra91151
      wrote on last edited by Cobra91151
      #14

      @SGaist

      I have tried the static method also, the same warning message still exists.

      Code (call to close db connection when app closes):

      void Test2::closeDBConnection()
      {
          Database *db = new Database();
          connect(db, &Database::finished, db, &Database::deleteLater);
          db->closeConnection();
      }
      
      void Test2::closeEvent(QCloseEvent *event)
      {
          closeDBConnection();
          event->accept();
          QWidget::closeEvent(event);
      }
      
      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #15

        Because you have somewhere in your code an instance of QSqlDatabase stored.

        From your code, I'd say you have a QSqlDatabase database member variable in your Database class. Remove that.

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

        Cobra91151C 2 Replies Last reply
        1
        • SGaistS SGaist

          Because you have somewhere in your code an instance of QSqlDatabase stored.

          From your code, I'd say you have a QSqlDatabase database member variable in your Database class. Remove that.

          Cobra91151C Offline
          Cobra91151C Offline
          Cobra91151
          wrote on last edited by Cobra91151
          #16

          @SGaist

          What do you mean by removing QSqlDatabase database member variable? When I removed it, I will no longer open/restore or use QSqlDatabase connection.

          I have changed code to:

             QSqlDatabase db = QSqlDatabase::database("TestConnection");
          
             if (db.isOpen()) {
                 db.close();
                 qDebug() << db.connectionName();
                 QSqlDatabase::removeDatabase(db.connectionName());
             }
          

          The issue still exists, I think you are right about removing QSqlDatabase member variable, I still get it by calling QSqlDatabase::database("TestConnection") and then all checks will proceed. I will try it.

          1 Reply Last reply
          0
          • SGaistS SGaist

            Because you have somewhere in your code an instance of QSqlDatabase stored.

            From your code, I'd say you have a QSqlDatabase database member variable in your Database class. Remove that.

            Cobra91151C Offline
            Cobra91151C Offline
            Cobra91151
            wrote on last edited by Cobra91151
            #17

            @SGaist

            I have deleted the QSqlDatabase database member variable, but the issue still exists. But I think I figured it out. I will test it and reply.

            Yes, it's finally working.

            Code:

               QSqlDatabase db = QSqlDatabase::database("TestConnection");
            
               if (db.isOpen()) {
                   QString dbConnectionName = db.connectionName();
                   db.close();
                   qDebug() << dbConnectionName;
                   db = QSqlDatabase();
                   db.removeDatabase(dbConnectionName); // or QSqlDatabase::removeDatabase(dbConnectionName);
               }
            
              emit finished();
            

            And now I use the connection to the DB until app closes or close it whenever I want. Also I would like to add, that I add check in the constructor and use database connection - QSqlDatabase db = QSqlDatabase::database("TestConnection"); in the methods because different methods can execute in different situations, so it will always create or use the connection to database.

            Database::Database(QObject *parent) : QObject(parent)
            {
                if (!checkDBConnection()) {
                     //Create db connection with the name "TestConnection"
                }
            }
            
            bool Database::checkDBConnection()
            {
                if (QSqlDatabase::contains("TestConnection")) {
                    return true;
                }
            
                return false;
            }
            

            Thank you all.

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

              Did you read the example code from the removeDatabase documentation I linked to earlier ?

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

              Cobra91151C 1 Reply Last reply
              0
              • SGaistS SGaist

                Did you read the example code from the removeDatabase documentation I linked to earlier ?

                Cobra91151C Offline
                Cobra91151C Offline
                Cobra91151
                wrote on last edited by
                #19

                @SGaist

                Yes, but for some reason I need to dereference the database object (db = QSqlDatabase();) to actually succeed with removing the DB connection even with the static method, otherwise I get error QSqlDatabasePrivate::removeDatabase: connection 'TestConnection' is still in use, all queries will cease to work..

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

                  Please show the complete code you are currently using.

                  You should also check if you have other QSqlDatabase objects lying around.

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

                  Cobra91151C 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Please show the complete code you are currently using.

                    You should also check if you have other QSqlDatabase objects lying around.

                    Cobra91151C Offline
                    Cobra91151C Offline
                    Cobra91151
                    wrote on last edited by Cobra91151
                    #21

                    @SGaist

                    I don't have any member variable, only local variable QSqlDatabase db = QSqlDatabase::database("TestConnection"); to get the DB connection and use it to execute queries. It should deletes itself when it goes out of scope. So I think no QSqlDatabase objects are lying around.

                    More info available - warning remove database

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

                      The correct way is described in the documentation of the function:

                      void someFunction()
                      {
                          {
                              QSqlDatabase db = QSqlDatabase::database("sales");
                              QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
                          }
                          // Both "db" and "query" are destroyed because they are out of scope 
                          QSqlDatabase::removeDatabase("sales"); // correct
                      }
                      

                      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