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. QSql QSqlDatabasePrivate::removeDatabase: connection 'IDconnect' is still in use, all queries will cease to work.

QSql QSqlDatabasePrivate::removeDatabase: connection 'IDconnect' is still in use, all queries will cease to work.

Scheduled Pinned Locked Moved Unsolved General and Desktop
qsql
16 Posts 2 Posters 7.7k 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    I have the following code to connect and disconnect from the database:

     db = QSqlDatabase::addDatabase ("QSQLITE","IDconnect");
        db.setDatabaseName (fileQstring );
    
        if(!db.open ())
            {
                qDebug() << "The database is NOT open!";
                QMessageBox::critical (this,"Error 1001","The database can't be opened!");
            }
        else
            {
                qDebug() << "The database is open (ID)!";
            }
    
        QSqlQuery query("SELECT ID FROM Items ORDER BY ID DESC",db);
    
        if(query.isActive()==true)
            {
                qDebug() << "The query is active (ID).";
            }
        else
            {
                qDebug() << "The query is NOT active(ID).";
            }
    
        query.first ();
    
        LastID = query.value(0).toInt ();
    
        db.close ();
        ItemID  = LastID+1;
    
        sID = QString::number(ItemID);
        ui->ID_Display ->setText (sID);
    
        db.close ();
    
        QSqlDatabase::removeDatabase ("IDconnect");
    
    

    I think I followed the recommendations on the Forum and Qt 5.7 help but I keep getting this error message. It doesn't cause any visible issues but I want to get rid of it. What's missing from the code?
    Thank you for your help.

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

      Hi,

      It's your db object that triggers that message.

      Take a look at the removeDatabase documentation to see how to handle your use case properly.

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

      G 2 Replies Last reply
      1
      • SGaistS SGaist

        Hi,

        It's your db object that triggers that message.

        Take a look at the removeDatabase documentation to see how to handle your use case properly.

        G Offline
        G Offline
        gabor53
        wrote on last edited by
        #3

        @SGaist
        Thank you. It worked.

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

          Then please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

          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
          1
          • G Offline
            G Offline
            gabor53
            wrote on last edited by
            #5

            Thank you. It worked.

            1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              It's your db object that triggers that message.

              Take a look at the removeDatabase documentation to see how to handle your use case properly.

              G Offline
              G Offline
              gabor53
              wrote on last edited by
              #6

              @SGaist
              Hi,
              I got back to the same problem.
              I realized that when Additem loads the first time everything works fine. I get the error message when Additem is reloaded from another class, Review. I assume, somehow the original Additem database connections are still active when I load the class the second time. How can I avoid recreating the connection each time I reload Additem?
              Thank you.

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

                Do you have several connections to the database ?

                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
                • G Offline
                  G Offline
                  gabor53
                  wrote on last edited by
                  #8

                  Yeswith different names.

                  1 Reply Last reply
                  0
                  • G Offline
                    G Offline
                    gabor53
                    wrote on last edited by
                    #9

                    I ment connection names. Also when Additem is called several times I assume it attempts to reconnect to the db and kills the previous connection with the same name.

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

                      You don't need to re-create the connection each time. You create it one time and then, depending on how your application works, you open and close it at will or open it once at the start of your application. When you want to use it, you can use the following construct:

                      void MyCoolClass::myCoolFunction()
                      {
                      QSqlDatabase db = QSqlDatabase::database("IDconnect");
                      // open if needed
                      QSqlQuery query(db);
                      // rest of your code
                      // close if needed
                      }
                      

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

                      G 1 Reply Last reply
                      0
                      • SGaistS SGaist

                        You don't need to re-create the connection each time. You create it one time and then, depending on how your application works, you open and close it at will or open it once at the start of your application. When you want to use it, you can use the following construct:

                        void MyCoolClass::myCoolFunction()
                        {
                        QSqlDatabase db = QSqlDatabase::database("IDconnect");
                        // open if needed
                        QSqlQuery query(db);
                        // rest of your code
                        // close if needed
                        }
                        
                        G Offline
                        G Offline
                        gabor53
                        wrote on last edited by
                        #11

                        @SGaist
                        Thank you. Do I close it the right way using db.close() and removeDatabase?

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

                          There's no need to call removeDatabase every time. QSqlDatabase can be seen as a sort of registry where you store all the connection data for the databases you are going to access. Unless you have good reasons to remove that particular connection, just set it up once and for the rest follow the myCoolFunction example.

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

                          G 1 Reply Last reply
                          0
                          • SGaistS SGaist

                            There's no need to call removeDatabase every time. QSqlDatabase can be seen as a sort of registry where you store all the connection data for the databases you are going to access. Unless you have good reasons to remove that particular connection, just set it up once and for the rest follow the myCoolFunction example.

                            G Offline
                            G Offline
                            gabor53
                            wrote on last edited by
                            #13

                            @SGaist
                            I made some changes. The db connection is used in the Additem class so I created a connection function like this:

                            void Additem::connection()
                            {
                                if(!db.open ())
                                    {
                                        db = QSqlDatabase::addDatabase ("QSQLITE","Friend");
                                        db.setDatabaseName (fileQstring );
                                    }
                            }
                            

                            This became the only db opening statement. I call this function before function Addfriend as Addfriend is the function where the db connection is used. Whenever Additem is called, I get the usual error messages:
                            QSqlDatabasePrivate::removeDatabase: connection 'Friend' is still in use, all queries will cease to work.
                            QSqlDatabasePrivate::addDatabase: duplicate connection name 'Friend', old connection removed.

                            Closing Additem clearly doesn't removes the connection (based on the error messages) but if there is no connection why does the !db.open thinks the connection is still open and gives me a new message? Thank you.

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

                              Again, you have a QSqlDatabase class member. Each time you create a Additem object you create a new invalid QSqlDatabase object that will be replaced in connection and depending on how Additem objects are managed, you'll have several of these connection re-created.

                              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
                              • G Offline
                                G Offline
                                gabor53
                                wrote on last edited by
                                #15

                                Is this something I definitely have to fix or it's not a big deal?

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

                                  Duplicating connection name means that you will break at least one connection. I'd rather not overlook that.

                                  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