July 21, 2010

rileo8 rileo8
Ant Farmer
53 posts

QSqlDatabasePrivate::removeDatabase

Page  
1

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.

18 replies

July 21, 2010

Tobias Hunger Tobias Hunger
Robot Herder
3387 posts

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

July 21, 2010

rileo8 rileo8
Ant Farmer
53 posts

I do like this:

when exiting:

  1.   conn->close();
  2.        logger->close();
  3.        logger->logOnFile("MainButton","mousePressEvent","Exiting from program.");
  4.        exit(0);
and the conn->close is here:

  1. bool MysqlConnector2::close()
  2. {
  3.     bool result=true;
  4.  
  5.     QString connection;
  6.     connection=db.connectionName();
  7.  
  8.     QSqlDatabase::removeDatabase(connection);
  9.  
  10.  
  11.     return result;
  12. }

so to me it seems correct…..

what the problem here?

Thanks!

July 21, 2010

Tobias Hunger Tobias Hunger
Robot Herder
3387 posts

Hmmm… I never used our SQL classes… but …

the documentation on connectionName says:

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.

July 21, 2010

rileo8 rileo8
Ant Farmer
53 posts

I did this:

  1. bool MysqlConnector2::close()
  2. {
  3.     bool result=true;
  4.  
  5.     QString connection;
  6.     connection=db.connectionName();
  7.     db.close();
  8.  
  9.     QSqlDatabase::removeDatabase(connection);
  10.  
  11.  
  12.     return result;
  13. }

but the warning is still here….

July 21, 2010

Tobias Hunger Tobias Hunger
Robot Herder
3387 posts

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

  1. void QSqlDatabase::removeDatabase ( const QString & connectionName )

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

July 21, 2010

rileo8 rileo8
Ant Farmer
53 posts

I read the documentiation but…i don’t understand how to apply the example to my situation…

here is my class .cpp

  1. #include "mysqlconnector2.h"
  2.  
  3. MysqlConnector2::MysqlConnector2()
  4. {
  5.  
  6. }
  7.  
  8. bool MysqlConnector2::open()
  9. {
  10.     bool result=false;
  11.  
  12.   {
  13.    db=QSqlDatabase::addDatabase("QMYSQL");
  14.    db.setHostName("localhost");
  15.    db.setDatabaseName("osty");
  16.    db.setUserName("root");
  17.    db.setPassword("root");
  18.  
  19.    if(db.open())
  20.    {
  21.        result=true;
  22.    }
  23.  
  24.   }
  25.     return result;
  26. }
  27.  
  28. bool MysqlConnector2::close()
  29. {
  30.     bool result=true;
  31.  
  32.     QString connection;
  33.     connection=db.connectionName();
  34.     db.close();
  35.  
  36.     QSqlDatabase::removeDatabase(connection);
  37.  
  38.  
  39.     return result;
  40. }
  41.  
  42.  
  43. QList < QHash <QString,QString> > MysqlConnector2::query_with_results(QString query)
  44. {
  45.    QList < QHash <QString,QString> > table;
  46.  
  47.    int i;
  48.    if(db.open())
  49.    {
  50.  
  51.       QSqlQuery qr=QSqlQuery(db);
  52.       qr.exec(query);
  53.  
  54.       QSqlRecord rec = qr.record();
  55.  
  56.       int columns=rec.count();
  57.  
  58.       while(qr.next())
  59.       {
  60.         QHash <QString,QString> row;
  61.         i=0;
  62.         for(i=0;i<columns;i++)
  63.         {
  64.           // std::cout<<"NUMBER: "<<qr.value(i).toDouble()<<std::endl;
  65.            // seems that qr.value(i).toString() is language dependent...
  66.            row[rec.fieldName(i)]=qr.value(i).toString();//.replace(",",".");
  67.           // std::cout<<"VALUE : "<<row[rec.fieldName(i)].toStdString()<<std::endl;
  68.         }
  69.  
  70.         table<<row;
  71.       }
  72.    }
  73.  
  74.   return table;
  75. }
  76.  
  77. QString MysqlConnector2::query(QString query)
  78. {
  79.    QString result="";
  80.  
  81.    if(db.open())
  82.    {
  83.       QSqlQuery qr=QSqlQuery(db);
  84.       if(!qr.exec(query))
  85.       {
  86.           result=qr.lastError().text();
  87.       }
  88.  
  89.    }
  90.   return result;
  91. }

July 21, 2010

Niak74 Niak74
Lab Rat
3 posts

Where do you call close() ?

Did you write any destructor for this class ?

EDIT : My way to do :

  1. MyClass::MyClass()
  2. {
  3.       m_db = QSqlDatabase::addDatabase("QMYSQL");
  4.       [...]
  5. }
  6.  
  7. MyClass::~MyClass
  8. {
  9.      QSqlDatabase::removeDatabase("QMYSQL");
  10. }
  11.  
  12. Type MyClass::aFunctionThatUsesDB(args)
  13. {
  14.      if(m_db.open()) {
  15.           [My stuff...]  
  16.           m_db.close();
  17.      }
  18.      else { displayError(); }
  19. }

July 21, 2010

rileo8 rileo8
Ant Farmer
53 posts

Here:

  1. bool MysqlConnector2::close()
  2. {
  3.     bool result=true;
  4.     QString connection;
  5.     connection=db.connectionName();
  6.     db.close();
  7.  
  8.     QSqlDatabase::removeDatabase(connection);
  9.  
  10.  
  11.     return result;
  12. }

July 21, 2010

Niak74 Niak74
Lab Rat
3 posts

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.

July 21, 2010

rileo8 rileo8
Ant Farmer
53 posts

Here i call the close():

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

just before exiting…

July 21, 2010

Luca Luca
Ant Farmer
637 posts

I do this:

  1. db->close();
  2. delete db;
  3. QSqlDatabase::removeDatabase(dbName);

July 21, 2010

rileo8 rileo8
Ant Farmer
53 posts

Thanks for the suggestion!

Doing like this it works:

  1. bool MysqlConnector2::close()
  2. {
  3.     bool result=true;
  4.  
  5.     QString connection;
  6.     connection=db.connectionName();
  7.     db.close();
  8.     delete &db;
  9.     QSqlDatabase::removeDatabase(connection);
  10.  
  11.  
  12.     return result;
  13. }

July 21, 2010

Tobias Hunger Tobias Hunger
Robot Herder
3387 posts

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.

July 21, 2010

rileo8 rileo8
Ant Farmer
53 posts

You are right,

that was only an attempt.

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

September 30, 2011

chikymanzanares chikymanzana..
Lab Rat
7 posts

finally, how do you solve the problem?

Page  
1

  ‹‹ QGraphicsItem movement      Extreme Programming with QtTest ››

You must log in to post a reply. Not a member yet? Register here!