QSqlDatabasePrivate::removeDatabase



  • Hi,

    how can i remove this warning message?

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


  • Moderators

    Just going by the message: Disconnect from the database before trying to do whatever is finally triggering the warning.



  • I do like this:

    when exiting:

     @  conn->close();
       logger->close();
       logger->logOnFile("MainButton","mousePressEvent","Exiting from program.");
       exit(0);
    

    @
    and the conn->close is here:

    @
    bool MysqlConnector2::close()
    {
    bool result=true;

    QString connection;
    connection=db.connectionName();
    
    QSqlDatabase::removeDatabase(connection);
    
    
    return result;
    

    }
    @

    so to me it seems correct.....

    what the problem here?

    Thanks!


  • Moderators

    Hmmm... I never used our SQL classes... but ...

    the documentation on connectionName says:

    bq. Returns the connection name, which may be empty. Note: The connection name is not the database name.

    So I think this is definitely not what you want.

    I think you need to get the database name, close the connection to the database (db.close()) and only afterwards remove the database.



  • I did this:

    @bool MysqlConnector2::close()
    {
    bool result=true;

    QString connection;
    connection=db.connectionName();
    db.close();
    
    QSqlDatabase::removeDatabase(connection);
    
    
    return result;
    

    }@

    but the warning is still here....


  • Moderators

    Looks like I have been wrong then:-( Try reading the documentation on

    @void QSqlDatabase::removeDatabase ( const QString & connectionName )@

    The example there does exactly match your situation from what I see.



  • I read the documentiation but...i don't understand how to apply the example to my situation...

    here is my class .cpp

    @#include "mysqlconnector2.h"

    MysqlConnector2::MysqlConnector2()
    {

    }

    bool MysqlConnector2::open()
    {
    bool result=false;

    {
    db=QSqlDatabase::addDatabase("QMYSQL");
    db.setHostName("localhost");
    db.setDatabaseName("osty");
    db.setUserName("root");
    db.setPassword("root");

    if(db.open())
    {
    result=true;
    }

    }
    return result;
    }

    bool MysqlConnector2::close()
    {
    bool result=true;

    QString connection;
    connection=db.connectionName();
    db.close();
    
    QSqlDatabase::removeDatabase(connection);
    
    
    return result;
    

    }

    QList < QHash <QString,QString> > MysqlConnector2::query_with_results(QString query)
    {
    QList < QHash <QString,QString> > table;

    int i;
    if(db.open())
    {

      QSqlQuery qr=QSqlQuery(db);
      qr.exec&#40;query&#41;;
    
      QSqlRecord rec = qr.record();
    
      int columns=rec.count();
    
      while(qr.next())
      {
        QHash <QString,QString> row;
        i=0;
        for(i=0;i<columns;i++)
        {
          // std::cout<<"NUMBER: "<<qr.value(i).toDouble()<<std::endl;
           // seems that qr.value(i).toString() is language dependent...
           row[rec.fieldName(i)]=qr.value(i).toString();//.replace(",",".");
          // std::cout<<"VALUE : "<<row[rec.fieldName(i)].toStdString()<<std::endl;
        }
    
        table<<row;
      }
    

    }

    return table;
    }

    QString MysqlConnector2::query(QString query)
    {
    QString result="";

    if(db.open())
    {
    QSqlQuery qr=QSqlQuery(db);
    if(!qr.exec(query))
    {
    result=qr.lastError().text();
    }

    }
    return result;
    }@



  • Where do you call close() ?

    Did you write any destructor for this class ?

    EDIT : My way to do :

    @
    MyClass::MyClass()
    {
    m_db = QSqlDatabase::addDatabase("QMYSQL");
    [...]
    }

    MyClass::~MyClass
    {
    QSqlDatabase::removeDatabase("QMYSQL");
    }

    Type MyClass::aFunctionThatUsesDB(args)
    {
    if(m_db.open()) {
    [My stuff...]
    m_db.close();
    }
    else { displayError(); }
    }
    @



  • Here:

    @bool MysqlConnector2::close()
    {
    bool result=true;
    QString connection;
    connection=db.connectionName();
    db.close();

    QSqlDatabase::removeDatabase(connection);
    
    
    return result;
    

    }
    @



  • Got it, but where do you call MysqlConnector2::close() (outside this class).

    Does the error message appear when you close your app, or when you call MysqlConnector2::close() method ? (verify this point using debug mode and breakpoints !)

    I recommand you to make atomic methods that implie to open AND close connection to your DB. By this way, you will be sure release your resources.

    See the EDIT in my previous message.



  • Here i call the close():

    @ conn->close();
    logger->close();
    logger->logOnFile("MainButton","mousePressEvent","Exiting from program."&#41;;
    exit(0);
    @

    just before exiting...



  • I do this:
    @
    db->close();
    delete db;
    QSqlDatabase::removeDatabase(dbName);
    @



  • Thanks for the suggestion!

    Doing like this it works:

    @bool MysqlConnector2::close()
    {
    bool result=true;

    QString connection;
    connection=db.connectionName();
    db.close();
    delete &amp;db;
    QSqlDatabase::removeDatabase(connection);
    
    
    return result;
    

    }@


  • Moderators

    delete &db is really aehm... ugly! You are calling the destructor on a stack-allocated object. This will lead to the destructor of db getting called twice, once when you do it and once when it goes out of scope. Undefined behaviour is lurking here... nothing I want close to my databases;-)

    You could hold a pointer to a db instead, initializing it like this db = new QSqlDatabase(QSqlDatabase::addDatabase(...));

    That pointer can be deleted properly in your close method.



  • You are right,

    that was only an attempt.

    Now i will modify the code to be less ugly..eheheh



  • finally, how do you solve the problem?



  • Nice, it works for me too, just by declaring a pointer instead of an object and using it like this:

    @
    m_db = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE"));
    m_szConnectionName = m_db->connectionName();
    ...
    m_db->open();
    ...
    m_db->close();
    ...
    delete m_db
    QSqlDatabase::removeDatabase(m_szConnectionName);
    @



  • how do you define m_db?



  • ok i found it
    QSqlDatabase *db;

    thanks it worked for me


Log in to reply
 

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