- 问题

  • 报错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);
}

注意事项

参考

注意:参考文章中不是完全正确的。

更多推荐