Hi
I m working on a wrapper class for a data base connection.
I have a member of class QSqlDatabase which is initialzed using the following call:

Qt Code:
  1. QSqlDatabase::addDatabase(dbDriverName,"QDB")
To copy to clipboard, switch view to plain text mode 

I connect to the data base and open the connection as following

Qt Code:
  1. db.setHostName("localhost");
  2. db.setDatabaseName("dbname");
  3. db.setUserName("dbuser");
  4. db.setPassword("dbpassword");
  5. db.open();
To copy to clipboard, switch view to plain text mode 
It's all fine. As you can see the connection uses a name "QDB".

I use QslQuery to do what I have to do with the data base and it fails because the connection is not opened (message from db.lastError())

At the end once I'm done, the data base connection is closed and the data base is removed using
Qt Code:
  1. db.close();
  2. QSqlDatabase::removeDatabase("QDB");
To copy to clipboard, switch view to plain text mode 

While trying to query the data base QsqlQuery::exec() fails because the connection is not opened.
The call to removeDataBase complains that the connection "QDB" is still use while supposedly it failed just before (for default connection ?!)


If I do the exact same things but instead using a named connection I used the default connection QsqlQuery::exec() proceeds normally but at the end while trying to remove the database I have a warning concerning the default connection "still in use".

I put a break point in Qt src code and to my surprise QslQuery increments a ref counter of QSqlDatabase() instances. If this counter is != of 1 while calling removeDatabase() for the default connection it complains ("still in use" warning).

For a slightly more simple code example every thing works fine but the final removeDatabase complaint ("still in use").
Here is the code I used:

Qt Code:
  1. QSqlDatabase db= QSqlDatabase::addDatabase("QMYSQL");
  2. db.setHostName("localhost");
  3. db.setDatabaseName("dbname");
  4. db.setUserName("dbuser");
  5. db.setPassword("dbpasword");
  6. db.open();
  7.  
  8. QSqlQuery query;
  9. QString s("DROP TABLE IF EXISTS TOTO");
  10. if(query.exec(s))
  11. {
  12. query.finish();
  13. s.clear();
  14. s.append("CREATE TABLE toto ");
  15. s.append("(id int(8) NOT NULL PRIMARY KEY,");
  16. s.append("name character varying(100));");
  17. if(!query.exec(s))
  18. cout<<"error for query :"<<s.toStdString()<<endl;
  19. }
  20. else
  21. cout<<"error for query :"<<s.toStdString()<<endl;
  22.  
  23. if(db.isOpen())db.close();
  24. db.QSqlDatabase::removeDatabase(db.connectionName());
To copy to clipboard, switch view to plain text mode 


So I come to my question.
What is the best way to use QslDatabase and QslQuery without getting this annoying warning message.