查看完整版本: [-- removeDatabase: connection 'mysqlLocal' is still in use, all queries will cease to work. --]

QTCN开发网 -> Qt基础编程 -> removeDatabase: connection 'mysqlLocal' is still in use, all queries will cease to work. [打印本页] 登录 -> 注册 -> 回复主题 -> 发表主题

介农酥 2019-06-20 10:59

removeDatabase: connection 'mysqlLocal' is still in use, all queries will cease to work.

这条报警有办法消除吗?
自己网上看了,也没有消除。

XChinux 2019-06-20 11:51
在退出程序之前,调用removeDatabase()删除连接
在调用removeDatabase()删除连接之前,销毁该连接的所有数据库相关类实例

介农酥 2019-06-20 13:05
怎么才算清掉所有相关的实例?
  1. #include "dialog.h"
    #include <QApplication>
    #include <QSqlDatabase>
    #include <QtDebug>
    #include <QSqlError>

    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);

        QSqlDatabase db;
        db = QSqlDatabase::addDatabase("QMYSQL", "mysqlLocal");
        db.setHostName("127.0.0.1");
        db.setDatabaseName("express_plc");
        db.setUserName("remoteuser");
        db.setPassword("8ik,9ol");
        if (!db.open()) {
            qDebug() << "database open failure";
            qDebug() << db.lastError().text();
        } else {
            qDebug() << "database open successful";
        }
        if (!db.open()) {
            qDebug() << "database second open failure";
            qDebug() << db.lastError().text();
        } else {
            qDebug() << "database second open successful";
        }
        db.close();
        if (db.isValid())
            qDebug("vaild!");
        //db.removeDatabase("mysqlLocal");

        {
            QString name;
            name = QSqlDatabase::database("mysqlLocal").connectionName();
            QSqlDatabase::removeDatabase(name);
        }


        if (db.isValid())
            qDebug("still vaild!");
        return a.exec();
    }
输出:
database open successful
database second open successful
vaild!
QSqlDatabasePrivate::removeDatabase: connection 'mysqlLocal' is still in use, all queries will cease to work.

我上面的代码,db都没有用来查询,我也关闭了,可是还报,真的不知道,怎样才算清掉所有相关的实例。
---------------------------------------------------------------------------------------------------------------------------------------------------------

XChinux 2019-06-20 15:24
比如db这个实例对象需要销毁
所以,你上面的代码,要么db改成指针形式,在调用removeDatabases()之前delete db
或者,让db作用域在调用removeDatabases()之前失效。
比如
{
      QSqlDatabase db;
      .....
}
QSqlDatabase::removeDatabases(....);

spygg 2019-06-24 20:09
最近刚好遇到这个问题,几经尝试终于解决详见我的博客
我的博客

介农酥 2019-07-01 15:50
谢谢!

介农酥 2019-07-01 16:03
XChinux:比如db这个实例对象需要销毁
所以,你上面的代码,要么db改成指针形式,在调用removeDatabases()之前delete db
或者,让db作用域在调用removeDatabases()之前失效。
比如
{
....... (2019-06-20 15:24) 

之前指针都不会用,基础不扎实啊,感谢大神!


查看完整版本: [-- removeDatabase: connection 'mysqlLocal' is still in use, all queries will cease to work. --] [-- top --]



Powered by phpwind v8.7 Code ©2003-2011 phpwind
Gzip disabled