QSqlDatabasePrivate::removeDatabase: connection ‘myConnection’ is still in use, all queries will cease to work

发布时间 2023-07-19 20:39:11作者: 伟大的厨师
1. 解决QSqlDatabasePrivate::removeDatabase: connection ‘myConnection’ is still in use, all queries will cease to work的问题

该问题主要是因为没有关闭之前的数据库连接,然后又需要创建新的数据库连接导致。解决方案:必须释放该连接的所有查询,即删除所有与该连接有关的query;同时在释放该连接时,需要先获取该连接的连接名,然后先关闭当前数据库,并将当前数据库使用默认构造函数覆盖,最后再移除当前连接。
解决代码:
void database::close()
{
QString connection;
connection = m_db.connectionName();
m_db.close();
m_db = QSqlDatabase();
m_db.removeDatabase(connection);
}
参考链接:http://stackoverflow.com/questions/9519736/warning-remove-database
http://stackoverflow.com/questions/8461333/sql-connection-still-open-even-after-deleting-it