QSql QSqlDatabasePrivate::removeDatabase: connection 'IDconnect' is still in use, all queries will cease to work.
-
Hi,
I have the following code to connect and disconnect from the database:db = QSqlDatabase::addDatabase ("QSQLITE","IDconnect"); db.setDatabaseName (fileQstring ); if(!db.open ()) { qDebug() << "The database is NOT open!"; QMessageBox::critical (this,"Error 1001","The database can't be opened!"); } else { qDebug() << "The database is open (ID)!"; } QSqlQuery query("SELECT ID FROM Items ORDER BY ID DESC",db); if(query.isActive()==true) { qDebug() << "The query is active (ID)."; } else { qDebug() << "The query is NOT active(ID)."; } query.first (); LastID = query.value(0).toInt (); db.close (); ItemID = LastID+1; sID = QString::number(ItemID); ui->ID_Display ->setText (sID); db.close (); QSqlDatabase::removeDatabase ("IDconnect");
I think I followed the recommendations on the Forum and Qt 5.7 help but I keep getting this error message. It doesn't cause any visible issues but I want to get rid of it. What's missing from the code?
Thank you for your help. -
Hi,
It's your
db
object that triggers that message.Take a look at the removeDatabase documentation to see how to handle your use case properly.
-
Then please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)
-
@SGaist
Hi,
I got back to the same problem.
I realized that when Additem loads the first time everything works fine. I get the error message when Additem is reloaded from another class, Review. I assume, somehow the original Additem database connections are still active when I load the class the second time. How can I avoid recreating the connection each time I reload Additem?
Thank you. -
Do you have several connections to the database ?
-
You don't need to re-create the connection each time. You create it one time and then, depending on how your application works, you open and close it at will or open it once at the start of your application. When you want to use it, you can use the following construct:
void MyCoolClass::myCoolFunction() { QSqlDatabase db = QSqlDatabase::database("IDconnect"); // open if needed QSqlQuery query(db); // rest of your code // close if needed }
-
There's no need to call removeDatabase every time. QSqlDatabase can be seen as a sort of registry where you store all the connection data for the databases you are going to access. Unless you have good reasons to remove that particular connection, just set it up once and for the rest follow the
myCoolFunction
example. -
@SGaist
I made some changes. The db connection is used in the Additem class so I created a connection function like this:void Additem::connection() { if(!db.open ()) { db = QSqlDatabase::addDatabase ("QSQLITE","Friend"); db.setDatabaseName (fileQstring ); } }
This became the only db opening statement. I call this function before function Addfriend as Addfriend is the function where the db connection is used. Whenever Additem is called, I get the usual error messages:
QSqlDatabasePrivate::removeDatabase: connection 'Friend' is still in use, all queries will cease to work.
QSqlDatabasePrivate::addDatabase: duplicate connection name 'Friend', old connection removed.
Closing Additem clearly doesn't removes the connection (based on the error messages) but if there is no connection why does the !db.open thinks the connection is still open and gives me a new message? Thank you. -
Again, you have a QSqlDatabase class member. Each time you create a Additem object you create a new invalid QSqlDatabase object that will be replaced in
connection
and depending on how Additem objects are managed, you'll have several of these connection re-created. -
Duplicating connection name means that you will break at least one connection. I'd rather not overlook that.