QT 关闭数据库 警告处理

 

 警告如下:

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

 

解决方案:

头文件部分:

class SqliteDB : public QObject

{
    public:
        SqliteDB();
        void connectDB();
        void closeDB();
    private:
        QSqlDatabase *db; //注意这里
};

 

源文件部分

#include "sqlitedb.h"

 
 
SqliteDB::SqliteDB()
{}
 
 
 
void SqliteDB::connectDB()
{
    db = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE"));
    db->setDatabaseName(DB_NAME);
    if(!db->open())
    {
        QMessageBox::critical(NULL, "conn to database...", "failed!");
    }
 
 
}
 
 
 
void SqliteDB::closeDB() //注意这个函数的实现
{
    QString dbname = db->connectionName();
    db->close();
    delete db;
    db = NULL;
    QSqlDatabase::removeDatabase(dbname);
}
 
 

 

经过上述处理,即可将警告处理掉!

你可能感兴趣的:(QT 关闭数据库 警告处理)