Solved QSqlDatabasePrivate::removeDatabase: connection 'myConnectionName' is still in use, all queries will cease to work.
-
I have a folder where i have a many databases. Some times may be deleted or added database to the folder.
So I use QTimer and read all databases.It is a my code:
this->timer = new QTimer(this); this->timer->setInterval(15000); connect(this->timer, &QTimer::timeout, this, [=]() { QString path = "C:\\Users\\User\\Desktop\\DAXI SMS SENDER\\SMSSenderAllBASE"; //QString path = qApp->applicationDirPath() + "\\SMSSenderAllBASE"; QDir recoredDir(path); QStringList allFiles = recoredDir.entryList(QDir::NoDotAndDotDot | QDir::System | QDir::Hidden | QDir::AllDirs | QDir::Files, QDir::DirsFirst); for (int i = 0; i < allFiles.size(); i++) { QString fullPath = path + "\\" + allFiles[i]; QString connectionName = allFiles[i]; connectionName = connectionName.remove(connectionName.size() - 4, 4); QSqlDatabase db = QSqlDatabase::addDatabase("QIBASE", connectionName); db.setDatabaseName(fullPath); db.setHostName("localhost"); db.setPort(3050); db.setUserName("SYSDBA"); db.setPassword("masterkey"); thrdHelperSendSMS *help = new thrdHelperSendSMS(db, this); connect(help, &thrdHelperSendSMS::helperFinished, this, [=](QString connectionName){ QSqlDatabase t_db = QSqlDatabase::database(connectionName); t_db.close(); QSqlDatabase::removeDatabase(connectionName); delete help; }); help->run(); } }); this->timer->start();
Yes I'm sure that the helperFinished signal will happen and this time I will not have any connection with this base.
EDIT:
If i removethrdHelperSendSMS *help = new thrdHelperSendSMS(db, this); connect(help, &thrdHelperSendSMS::helperFinished, this, [=](QString connectionName){ QSqlDatabase t_db = QSqlDatabase::database(connectionName); t_db.close(); QSqlDatabase::removeDatabase(connectionName); delete help; }); help->run();
example:
for (int i = 0; i < allFiles.size(); i++) { QString fullPath = path + "\\" + allFiles[i]; QString connectionName = allFiles[i]; connectionName = connectionName.remove(connectionName.size() - 4, 4); QSqlDatabase db = QSqlDatabase::addDatabase("QIBASE", connectionName); db.setDatabaseName(fullPath); db.setHostName("localhost"); db.setPort(3050); db.setUserName("SYSDBA"); db.setPassword("masterkey"); QSqlDatabase::removeDatabase(connectionName); }
I have the same error.
-
@SGaist
Yes i using it wrong.Wrong use
QSqlDatabase db = QSqlDatabase::database("sales"); QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db); QSqlDatabase::removeDatabase("sales"); // will output a warning
Correct use
{ QSqlDatabase db = QSqlDatabase::database("sales"); QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db); } // Both "db" and "query" are destroyed because they are out of scope QSqlDatabase::removeDatabase("sales"); // correct
-
Hi,
Take a look at the removeDatabase documentation for an example of how to implement it properly.
-
@SGaist
Yes i using it wrong.Wrong use
QSqlDatabase db = QSqlDatabase::database("sales"); QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db); QSqlDatabase::removeDatabase("sales"); // will output a warning
Correct use
{ QSqlDatabase db = QSqlDatabase::database("sales"); QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db); } // Both "db" and "query" are destroyed because they are out of scope QSqlDatabase::removeDatabase("sales"); // correct