当前位置: 首页 > news >正文

Qt使用QSqlDatabase remoeDatabase()连接提示仍在使用解决方案

问题描述

调用QSqlDatabase的removeDatabase函数的时候,出现了如下错误

QSqlDatabasePrivate::removeDatabase: connection '05465461654654' is still in use, all queries will cease to work

官方示例

[static] void QSqlDatabase::removeDatabase(const QString &connectionName)
Removes the database connection connectionName from the list of database connections.
Warning: There should be no open queries on the database connection when this function is called, otherwise a resource leak will occur.
Example:// WRONGQSqlDatabase db = QSqlDatabase::database("sales");QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);QSqlDatabase::removeDatabase("sales"); // will output a warning// "db" is now a dangling invalid database connection,// "query" contains an invalid result setThe correct way to do it:{QSqlDatabase db = QSqlDatabase::database("sales");QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);}// Both "db" and "query" are destroyed because they are out of scopeQSqlDatabase::removeDatabase("sales"); // correctTo remove the default connection, which may have been created with a call to addDatabase() not specifying a connection name, you can retrieve the default connection name by calling connectionName() on the database returned by database(). Note that if a default database hasn't been created an invalid database will be returned.
Note: This function is thread-safe.

解决方案

{
QSqlDatabase db = QSqlDatabase::database(“sales”);
QSqlQuery query(“SELECT NAME, DOB FROM EMPLOYEES”, db);
}
// Both “db” and “query” are destroyed because they are out of scope
QSqlDatabase::removeDatabase(“sales”);

http://www.lryc.cn/news/164296.html

相关文章:

  • 管易云与金蝶云星空对接集成仓库查询打通仓库新增
  • ubuntu 安装 Mongodb 4.0、4.2、4.4
  • 详解Hugging Face Transformers的TrainingArguments
  • 【LeetCode-中等题】17. 电话号码的字母组合
  • 读高性能MySQL(第4版)笔记06_优化数据类型(上)
  • mac如何创建mysql数据库
  • Ceph入门到精通-centos8 install brctl
  • sqli第一关
  • 入行IC | 新人入行IC选择哪个岗位更好?
  • 时间旅行的Bug 奇怪的输入Bug
  • 解决nbsp;不生效的问题
  • 【Lidar】Cloud Compare介绍安装包
  • Java中的Maven是什么?
  • 计算机操作系统
  • 海学会读《乡村振兴战略下传统村落文化旅游设计》2023年度许少辉八一新书
  • tkinter树形图组件
  • 多线程的创建
  • 【django】APPEND_SLASH 路由末尾的斜杠问题
  • iOS16.0:屏幕旋转
  • Carla学习笔记(二)服务器跑carla,本地运行carla-ros-bridge并用rviz显示
  • 数学建模--退火算法求解最值的Python实现
  • 地理地形sdk:Tatuk GIS Developer Kernel for .NET Crack
  • Day_81-87 CNN卷积神经网络
  • 关于mybatisplus报错:Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplat的问题
  • Spring AOP基础动态代理基于JDK动态代理实现
  • 第一章 计算机系统概述 五、中断和异常、系统调用
  • 【C语言】文件操作(上)
  • 【Linux】让笔记本发挥余热,Ubuntu20.04设置WiFi热点
  • 【云平台】遥感地信云平台收录
  • 23种设计模式之---单例模式