欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

QT编译MySQL驱动/QT连接MySQL

程序员文章站 2022-06-04 13:36:25
...

QSqlDatabase: QMYSQL driver not loaded

花了一天时间在网上搜了很多资料,多半是水贴,解决不了问题,有很多帖子说是把安装的MySQL里面的libmysql.dll拷贝到QT的bin目录下去,我试了很多次,也装了好几个版本的MySQL有32位也有64位的,结果都于事无补,后来在Stack Overflow上看到一篇帖子(Qt: QSqlDatabase: QMYSQL driver not loaded

You need to build the QMYSQL driver by yourself first.
When you download Qt you have also the source that are provided with it.
In my case, it is in : C:\Qt\5.9\Src\qtbase\src\plugins\sqldrivers\mysql Here you have a *.pro (that is a kind of makefile generator). You just have to open it with QtCreator, Compile it and you will get a dll. This dll is the MySQL driver and must be in the same dirent of your application.
To do that, if you have a problem with the compilation (like mysql.h not found), you must “install” mysql library also.
In Linux you can have the same issue as well.

于是顺着这个思路开始编译MySQL驱动,感谢博主@swz5637QT5.13.2经历万般苦难终于编译成功MySQL驱动了经验贴,下面为我的总结

环境

我的安装环境:
windows10 pro
Qt Creator 4.10.2 Based on Qt 5.13.2 (MSVC 2017, 32 bit)
Server version: 8.0.20 MySQL Community Server - GPL

编译驱动

1、打开QT sqldrivers的源码,我的路径是C:\Qt\Qt5.12.6\5.12.6\Src\qtbase\src\plugins\sqldrivers\mysql
,如果安装QT时没有勾选安装源码这一选项的话,直接去官网下载自己的QT对应的版本官方链接,选择single文件,选择qtxxxx-src-版本.zip,解压打开,然后找到plugins/sqldrivers/mysql就可以看到源码了
2、用QT打开驱动源码
工程目录如下:
QT编译MySQL驱动/QT连接MySQL
如果直接编译的话,是会出错的,需要在.pro文件添加INCLUDEPATH和LIBS才可以
3、添加include和lib:

INCLUDEPATH +="C:/Program Files/MySQL/MySQL Server 8.0/include"
LIBS +="C:/Program Files/MySQL/MySQL Server 8.0/lib/libmysql.lib"

自己选择好MySQL的安装目录
4、应该是QT的加载代码原因,此时直接编译还是会出错
需要将.pro文件该行注释掉

#QMAKE_USE += mysql

5、此时编译会出现找不到或没有qsqldriverconfig.pri(具体文件名字我忘了,是qxxconfig文件)的错误,这个时候要更改qsqldriverbase.pri文件中include一项,我的代码如下

QT  = core core-private sql-private

# For QMAKE_USE in the parent projects.
include($$shadowed($$PWD)/configure.pri)

PLUGIN_TYPE = sqldrivers
load(qt_plugin)

DEFINES += QT_NO_CAST_TO_ASCII QT_NO_CAST_FROM_ASCII

configure.pri和qsqldriverbase.pri我都放在了工程目录下,具体的路径请读者根据自己的工程自行修改
6、这个时候就可编译通过了,然后会生成一个plugins文件里面有编译好的MySQL驱动
QT编译MySQL驱动/QT连接MySQL
这个时候就和网上帖子一样了,把编译好的这几个文件放到QT的plugins/sqldrivers目录下,如
QT编译MySQL驱动/QT连接MySQL
这个时候在做一步处理,将MySQL安装目录下的libmysql.dll拷贝到QT的mingw73_64/bin目录下,就完成了
QT编译MySQL驱动/QT连接MySQL

连接MySQL

void MainWindow::connectMysql()
{
    db = QSqlDatabase::addDatabase("QMYSQL");  //数据库驱动
    db.setHostName("127.0.0.1");      //数据库主机名localhost)
    db.setPort(3306);                 //端口
    db.setDatabaseName("helloworld");  //数据库名
    db.setUserName("root");           //用户名
    db.setPassword("071139");         //密码
    db.open();

    //测试
    if(!db.open())
    {
        qDebug()<<"Connect Error:"<<db.lastError().text();
        return ;
    }
    else
    {
         qDebug()<<"Connect Success";
    }
    //数据库查询
    QSqlQuery query(db);
    query.exec("select * from student"); //表名
    while(query.next())
    {
        qDebug()<<query.value("sname").toString(); //选择sname列
    }
    db.close();
}
//这里是查询可支持的数据库驱动有哪些
QStringList drivers = QSqlDatabase::drivers();
    foreach(QString driver,drivers)
        qDebug()<<driver;

本文结束,如果想编译oracle数据库驱动可以查看该篇文章:简书
@惘客 Qt编译oracle驱动

相关标签: mysql qt