qt6.8安装mysql8.0驱动
qt6.8本身是不带mysql驱动。想要在qt里面使用mysql,还是比较麻烦的。需要自己编译驱动
-
首先下载qt源码,链接Index of /archive/qt/6.8/6.8.1/single
-
下载mysql对于驱动文件,链接是MySQL :: Download MySQL Connector/C (Archived Versions)
-
编译qt sqldrivers,在qt creator里面打开qtbase-everywhere-src-6.8.1。使用Qt Creator 10.0.0 (Community)打开当前目录下的CMakeLists.txt文件,文件顶部追加三行设置
SET(FEATURE\_sql\_mysql ON) SET(MySQL\_INCLUDE\_DIR "C:/MySQL\_Server\_8.0/include") SET(MySQL\_LIBRARY "C:/MySQL\_Server\_8.0/lib/libmysql.lib")```
-
选择sqldrive进行编译
-
选择编译好的文件qsqlmysql.dll。将其放到qt的安装目录C:\Qt\6.8.1\msvc2022_64\plugins\sqldrivers(这个是的我的Qt安装目录)
此时如果重启qt,输入代码进行测试
#include <QSqlDatabase>
#include <QtDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QStringList drivers = QSqlDatabase::drivers(); //获取到qt中所支持的数据库类型
foreach(QString driver,drivers)
{
qDebug()<<driver;
}
return a.exec();
}
如果输出有QMYSQL,那说明安装成功了。
尝试链接mysql,并尝试open一下,如果不行。查看报错,如果报错这个
Authentication plugin 'caching_sha2_password' cannot be loaded: ????????????顣\r\nQMYSQL: Unable to connect
试试这个方法,借鉴别人大佬的方法
将mysql的bin目录内的libcrypto-1_1-x64.dll、libprotobuf.dll、libssl-1_1-x64.dll,plugin目录中的authentication_fido_client.dll、authentication_kerberos_client.dll、authentication_oci_client.dll连同lib内的libmysql.dll一同复制到Qt的bin目录下:C:\Qt\6.8.1\msvc2022_64\bin或者是自己开发的程序的运行目录。