当前位置:网站首页>QT连接sqlite数据库的错误及其修改办法
QT连接sqlite数据库的错误及其修改办法
2022-07-27 06:10:00 【hawanglc】
今天在使用qt操作数据库。因为是第一次用,所以,遇到了一些坑,记录一下。
系统:macOS
语言:QT 5.12
错误1:找不到驱动
错误提示:QSqlError("", "Driver not loaded", "Driver not loaded")
原因
在写操作数据库的代码的时候,首先需要一个数据库连接,如下:
QSqlDatabase::addDatabase("QSQLITE");
此时,默认的数据库连接名字是"qt_sql_default_connection"。
如果项目的配置不做调整的话,可执行程序是找不到数据库驱动的。
解决办法
打开qt项目的配置,在环境变量里加上驱动的路径配置。我这边的配置信息如下:
QT_PLUGIN_PATH=/Users/huGuohua/Qt5.12.0/5.12.0/clang_64/plugins
其中,上述目录的子目录sqldrivers里面就有各种数据库连接的驱动。
问题2:无法打开数据库
错误提示:QSqlQuery::exec: database not open
原因
在定义一个QSqlQuery变量的时候,需要有一个参数,这个参数就是用来指定对应的dml去使用哪个数据库连接的。如果定义的时候没有参数,程序也不提示错误,但是这个dml就孤零零地没有和数据库连接关联起来。也就出现了上面的错误。
解决办法
在定义dml的时候,需要马上和对应的数据库连接关联上。即如下:
QSqlQuery sql_update(database);
错误3:参数绑定的数量不匹配
错误提示:QSqlError("", "Parameter count mismatch", "")
原因
以下是不会出错的流程,如果不按这个流程,都可能会导致上面的错误出现。
正常的需要参数绑定的时候,代码的流程是:定义 QSqlQuery,然后最好clear()一下,接着用prepare()来处理对应的SQL。SQL中的参数的写法有两种:a)问号作为占位符:此时要使用addBindValue来将变量一个一个的绑定;b)冒号+名字作为占位符:此时要使用bindValue(":name",value)的方式来进行变量绑定;然后调用exec来执行。exec有很多写法,在参数绑定的时候,exec()中就不要写上SQL语句本身了,这个很关键。如果exec()中含有SQL语句的话,即使上面的流程再正确,参数绑定的再好,也是没有用的,exec就直接使用了参数中的SQL了。
边栏推荐
- Drools(5):Drools高级语法
- How does golang assign values to empty structures
- tableau prep连接maxcompute,只是书写很简单的sql,为啥报这个错误呢?
- Calledprocesserror during pre commit install
- Es compares the data difference between the two indexes
- How to submit C4d animation to cloud rendering farm for fast rendering?
- Reflection on pytorch back propagation
- Student status management system based on SSM
- 一款开源 OA 办公自动化系统
- Drools (5): drools advanced syntax
猜你喜欢

Relevant principles of MySQL index optimization

零号培训平台课程-1、SQL注入基础

MySQL: 提高最大连接数

The possibility of metauniverse from the perspective of technical principles: how Omniverse "built" Mars

C语言程序设计 | 程序编译与预处理

Visual horizontal topic bug1:filenotfounderror: could not find module 'mvcameracontrol dll‘ (or one of it

ESP8266(ESP-12F) 第三方库使用 -- SparkFun_APDS9960 (手势识别)

jjwt 生成token

Consideration on how the covariance of Kalman filter affects the tracking effect of deepsort

Overall dichotomy?
随机推荐
Misunderstanding of slice slice in golang
请问 mysql timestamp(6) 用flink-sql接过来是 null,这点有办法处理不
Golang encapsulates the packages involved in MySQL and the differences between sqlx and Gorm
[Vani has a date] tail on rainy days
How to submit C4d animation to cloud rendering farm for fast rendering?
用typescript实现排序-递增
SQLite 常用功能整合
Binary tree -- natural search semantics (1) Basics
Gbase 8C product introduction
Watermelon book chapter 3 - linear model learning notes
C# 常用功能整合-2
Knowledge points and answers of PHP intermediate interview in 2020
Pg_relation_size 问题
12. Integer to Roman整数转罗马数字
把Excel转换成CSV/CSV UTF-8
Sort increment with typescript
sql-labs SQL注入平台-第1关Less-1 GET - Error based - Single quotes - String(基于错误的GET单引号字符型注入)
查看服务器重启前的 dmesg 日志
在rhel8上使用soci连接oracle和postgresql和sqlite
Interpretation of deepsort source code (VII)