【Qt】sqlite多线程报错,removeDatabase: connection ‘xxx‘ is still in use, all queries will cease to work.
·
- 问题
- 报错1:
QSqlDatabasePrivate::database: requested database does not belong to the calling thread. - 报错2:
QSqlDatabasePrivate::removeDatabase: connection 'Polarimeter-Results' is still in use, all queries will cease to work.
- 问题1 解决
QSqlDatabasePrivate::database: requested database does not belong to the calling thread.
多线程程序中,每次使用完数据库需要使用QSqlDatabase::removeDatabase(connection);断开连接。
但是断开连接后,又出现了问题2
- 问题2 解决
QSqlDatabasePrivate::removeDatabase: connection ‘Polarimeter-Results’ is still in use, all queries will cease to work.
断开连接前,被全局变量m_db赋空值。
代码
void Xxx::sqliteClose()
{
QString connection = m_db.connectionName();
m_db.close();
m_db = QSqlDatabase(); //重新赋值
QSqlDatabase::removeDatabase(connection);
}
注意事项
m_db是全局变量,m_db不要作为参数传递,不然仍然报错2m_db如果使用局部变量则可以参考官方的解决方案【Qt中使用QSqlDatabase::removeDatabase()的正确方法,后半部分,前半部分不对】。
参考
注意:参考文章中不是完全正确的。
更多推荐


所有评论(0)