cease:终止
观察这段代码,它会导致上面的警告产生。
QSqlDatabase database;
if (QSqlDatabase::contains("connection"))
{
database = QSqlDatabase::database("connection");
}
else {
database = QSqlDatabase::addDatabase("QSQLITE", "connection");
database.setDatabaseName("student.sqlite");
}
if (!database.open())
{
qDebug() << "Error: Failed to connect database." << database.lastError();
}
else {
QSqlQuery query(database);
query.exec("select * from menu");
if (!query.exec())
{
qDebug() << "Error:" << query.lastError();
}
else {
while (query.next())
{
qDebug() << query.value("name").toString();
}
qDebug() << query.size();
}
database.close();
}
QSqlDatabase::removeDatabase("connection");
末尾调用了QSqlDatabase::removeDatabase("connection");
此方法内部实现中应该调用了QSqlDatabasePrivate::removeDatabase
提示:‘connection’连接仍然在使用,这时候removeDatabase,所有查询都停止工作。
{
QSqlDatabase database;
if (QSqlDatabase::contains("connection"))
{
database = QSqlDatabase::database("connection");
}
else {
database = QSqlDatabase::addDatabase("QSQLITE", "connection");
database.setDatabaseName("identifier.sqlite");
}
if (!database.open())
{
qDebug() << "Error: Failed to connect database." << database.lastError();
}
else {
QSqlQuery query(database);
query.exec("select * from kmd_menu");
if (!query.exec())
{
qDebug() << "Error:" << query.lastError();
}
else {
while (query.next())
{
//qDebug() << query.value("name").toString();
}
qDebug() << query.size();
}
database.close();
}
}
QSqlDatabase::removeDatabase("connection");
这样写就可以了,不弹出上面的提示信息了。
之前在网上查过,有个网友分享的,就是在调用QSqlDatabase::removeDatabase("connection");前析构掉QSqlDatabase对象database。
突然发现我一年前写过一篇类似的文章:
探究QSqlDatabase::removeDatabase_qsqldatabaseprivate::removedatabase: connection 's-CSDN博客