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 warning message
Forum Updated to NodeBB v4.3 + New Features

QSqlDatabase warning message

Scheduled Pinned Locked Moved Unsolved General and Desktop
qsqldatabase
6 Posts 4 Posters 2.3k Views 3 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.
  • G Offline
    G Offline
    gabor53
    wrote on last edited by
    #1

    Hi,
    I use the following to connect to a db:

    db = QSqlDatabase::addDatabase ("QSQLITE","What");
            db.setDatabaseName (fileQstring );
    

    When I'm done using the database I close the connection using

    QSqlDatabase::removeDatabase ("What");
    

    I keep getting the following warning message in Application Output:
    "QSqlDatabasePrivate::removeDatabase: connection 'What' is still in use, all queries will cease to work."
    What am I missing that causes this message?
    Thank you.

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

      You probably have open queries. Check in the documentation (http://doc.qt.io/qt-5.6/qsqldatabase.html#removeDatabase):
      "Warning: There should be no open queries on the database connection when this function is called, otherwise a resource leak will occur."

      https://forum.qt.io/topic/113070/qt-code-of-conduct

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

        Hi,

        To add to @jsulm, is your db object a member of one of your class ? If so, then there's your problem. You don't have to keep such an object around especially if you only use the default connection.

        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
        3
        • SGaistS SGaist

          Hi,

          To add to @jsulm, is your db object a member of one of your class ? If so, then there's your problem. You don't have to keep such an object around especially if you only use the default connection.

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

          @SGaist
          I defined the db in additem.h:

          QSqlDatabase db;
          

          so I assume its is part of the additem class. Should I just define it in additem.cpp?
          I think my code follows the documentation, but I still get the error message
          "QSqlDatabasePrivate::removeDatabase: connection 'Material' is still in use, all queries will cease to work".

                 {
                     db = QSqlDatabase::addDatabase ("QSQLITE","Material");
                     db.setDatabaseName (fileQstring );
          
                     if(!db.open ())
                     {
                         qDebug() << "The database is NOT open! (Material)";
                     }
                     else
                     {
                         qDebug() << "The database is open (Material)!";
                     }
          
                     QSqlQuery query_material ("SELECT Material FROM MaterialTable ORDER BY Material asc",db);
          
                     if(query_material.isActive()==true)
                     {
                         qDebug() << "The query is active (Material).";
                     }
                     else
                     {
                         qDebug() << "The query is NOT active (Material).";
                     }
          
                     while (query_material.next ())
                     {
          
                         material = query_material.value(0).toString();
          
                         ui->Material_Combo->addItem(material);
                     }
          
          
                     material = ui->Material_Combo->currentText ();
          
                     if(material.isEmpty ())
                     {
                         qDebug() << "Material is empty!!!" ;
                     }
                     qDebug() << "Material in loop: " << material;
                     db.close ();
                 }
                 QSqlDatabase::removeDatabase ("Material");
                 ui->Material_Combo->clearEditText ();
                 connect (ui->Material_Combo,SIGNAL(currentIndexChanged(int)),this,SLOT(getMaterial(int)));
          

          What should I do to stop the error message? Which query is still running?
          Thank you.

          the_T 1 Reply Last reply
          0
          • G gabor53

            @SGaist
            I defined the db in additem.h:

            QSqlDatabase db;
            

            so I assume its is part of the additem class. Should I just define it in additem.cpp?
            I think my code follows the documentation, but I still get the error message
            "QSqlDatabasePrivate::removeDatabase: connection 'Material' is still in use, all queries will cease to work".

                   {
                       db = QSqlDatabase::addDatabase ("QSQLITE","Material");
                       db.setDatabaseName (fileQstring );
            
                       if(!db.open ())
                       {
                           qDebug() << "The database is NOT open! (Material)";
                       }
                       else
                       {
                           qDebug() << "The database is open (Material)!";
                       }
            
                       QSqlQuery query_material ("SELECT Material FROM MaterialTable ORDER BY Material asc",db);
            
                       if(query_material.isActive()==true)
                       {
                           qDebug() << "The query is active (Material).";
                       }
                       else
                       {
                           qDebug() << "The query is NOT active (Material).";
                       }
            
                       while (query_material.next ())
                       {
            
                           material = query_material.value(0).toString();
            
                           ui->Material_Combo->addItem(material);
                       }
            
            
                       material = ui->Material_Combo->currentText ();
            
                       if(material.isEmpty ())
                       {
                           qDebug() << "Material is empty!!!" ;
                       }
                       qDebug() << "Material in loop: " << material;
                       db.close ();
                   }
                   QSqlDatabase::removeDatabase ("Material");
                   ui->Material_Combo->clearEditText ();
                   connect (ui->Material_Combo,SIGNAL(currentIndexChanged(int)),this,SLOT(getMaterial(int)));
            

            What should I do to stop the error message? Which query is still running?
            Thank you.

            the_T Offline
            the_T Offline
            the_
            wrote on last edited by
            #5

            @gabor53

            You need to add the database connection only once, for example in your class constructor

            AddItem::AddItem(QString fileQString) { //I dont know how it really looks like :)
              QSqlDatabase db = QSqlDatabase::addDatabase ("QSQLITE","Material");
              db.setDatabaseName (fileQstring );
              if(!db.open()) 
                //do some error handling here
            }
            

            Then in your other functions where you need to do some database thingies:

            void AddItem::somefunction() {
             QSqlDatabase db = QSqlDatabase::database("Material");
              //the database is still open if you did not close it somewhere else, and the databaseName is still what you set
              QSqlQuery q("",db);
              q.prepare("select * from mytable"); // or whatever you need to do
            //...
            }
            

            And finally, close the database and remove it in the descturctor

            void AddItem::closedbcon() {
              QSqlDatabase db = QSqlDatabase::database("Material");
              db.close();
            }
            
            AddItem::~AddItem() {
              closedbcon();
              QSqlDatabase::removeDatabase ("Material");
            }
            

            -- No support in PM --

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

              To add to @the_ , if you only have one database connection and don't give it a name, it becomes the default connection and you don't need to call QSqlDatabase::database each time you make a query.

              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

              • Login

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