QSqlDatabase warning message



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


  • Moderators

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


  • Lifetime Qt Champion

    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.



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



  • @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");
    }
    

  • Lifetime Qt Champion

    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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.