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.
  • 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