脱坑篇
Qt连接MySQL数据库说起来很简单,但是做起来,,,,,,,emmm,我是不想说什么了。首先是新版Qt没有win64版本,想用的话需要自行编译(很难,我试过了,各种报错),其次查遍全网(有点夸张,不过差不多了),有说复制MySQL驱动到某某文件夹的,有说手动编译驱动的(同样各种报错),有说MySQL版本和Qt版本不一致(64 bit 和 32 bit 不兼容,这种说法个人看是正确的)。无论如何,最终导致的结果都是"QSqlDatabase: QMYSQL driver not loaded"。
之所以说这是脱坑篇,是因为,本人已经暂时放弃使用Qt直接连接MySQL数据库了,中途想过换个数据库的方法,其实这个方法可行!
思前想后,我采用了比较圆滑的一种方式。。。。emmm,用ODBC桥接MySQL,我喜欢这么说,大概就是这个意思吧。
步骤如下:win10系统可能没有自带ODBC的MySQL驱动,所以,需要手动下载安装驱动。这个很简单的,百度一搜“一大把”。
(不想写了,那找个链接吧。。。。。。)
Qt使用ODBC连接MySQL(点这个奥)
跟随这位脱坑大师的步伐,大家一起脱坑吧。
下边简单附上一些必要的代码。
QT += core gui sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated A