QSqlDatabase: How to avoid 'qt_sql_default_connection' still in use & duplicate connection related warning?

前端 未结 1 2014
北荒
北荒 2021-01-14 21:58

Sorry if this is a trivial question but I have been trying to build a small .ui that used QSQLITE as database and that uses QTableView to show 4 co

相关标签:
1条回答
  • 2021-01-14 22:37

    Short answer

    • You should specify the database you want to run your QSqlQuery on, otherwise they will run on the default database. You can specify the database in QSqlQuery's constructor with

      QSqlQuery query(QSqlDatabase::database("my-db"));

    • You are keeping a copy of the QSqlDatabase as a member of your dataInfo class, which will prevent it prevent closing properly. Instead, just use the static QSqlDatabase::database("name") when needed.

      auto db = QSqlDatabase::database("my-db");

    Details

    Providing the right database to QSqlQuery

    Change all your uses of QSqlQuery. For example for confDataBase:

    bool dataInfo::confDataBase()
    {
        // Explicitly provide your database to the query
        // Otherwise the default database is used
        QSqlQuery qry(getDatabase());
        bool ok = qry.exec(CREATE_TABLE);
        if(!ok) {
            mError = qry.lastError().text();
        }
        return ok;
    }
    

    Not keeping QSqlDatabase attributes

    From the documentation:

    Warning: It is highly recommended that you do not keep a copy of the QSqlDatabase around as a member of a class, as this will prevent the instance from being correctly cleaned up on shutdown. If you need to access an existing QSqlDatabase, it should be accessed with database(). If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior.

    Store your database's name in your class instead, and change your getDatabase to

    dataInfo.cpp

    bool dataInfo::initDataBase(const QString &nameDB)
    {
        // Save database's name
        mDBName = nameDB;
        // Use the database locally, without storing it
        auto dbImages = QSqlDatabase::addDatabase("QSQLITE", nameDB);
        bool ok = dbImages.open();
        if(!ok) {
            mError = dbImages.lastError().text();
            qDebug() << mError;
        }
        return ok;
    }
    
    QSqlDatabase dataInfo::getDatabase()
    {
        return QSqlDatabase::database(mDBName);
    }
    

    dataInfo.h

    private:
        QString mError;
        QString mDBName;
    

    Qt's code generating the warning

    To have a look at the actual code producing the error: https://code.woboq.org/qt5/qtbase/src/sql/kernel/qsqldatabase.cpp.html#170

    invalidateDb is used when connections are added or removed, and will trigger the error if the reference count > 1. As you are holding onto one, this will trigger the error.

    0 讨论(0)
提交回复
热议问题