admin

QSqlDatabasePrivate :: removeDatabase:连接“ myConnectionName”仍在使用中,所有查询将停止工作

sql

我有一个文件夹,其中有很多数据库。有时可能会删除数据库或将数据库添加到该文件夹​​。因此,我使用QTimer并读取所有数据库。

这是我的代码:

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();

是的,我确定将出现helperFinished信号,这一次我将与该基准站没有任何关系。

编辑: 如果我删除

   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();

例子:

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);
}

我有同样的错误。


阅读 519

收藏
2021-07-01

共1个答案

admin

您使用不removeDatabase()正确。SqlDatabase的对象首先需要超出范围。请参阅文档

使用错误

QSqlDatabase db = QSqlDatabase::database("sales");
QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
QSqlDatabase::removeDatabase("sales"); // will output a warning

正确使用

{
    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

在第二个示例中,db将超出范围after },您将不再看到错误消息QSqlDatabasePrivate::removeDatabase: connection 'myConnectionName' is still in use, all queries will cease to work

请仔细阅读文档。数据库内容是明智的,需要仔细检查每一行。

你也失踪db.close(); -在删除数据库之前,请先关闭数据库。

2021-07-01