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

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

posted @   伟大的厨师  阅读(1023)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示