安装Qt新版本的时候,会发现里面没有QMYSQL,所以要使用QMYSQL的时候会报以下的错误
QSqlDatabase: QMYSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QODBC QODBC3 QPSQL QPSQL7
这时候,就要自己编译源码了。这里有一篇博客可以参考
https://www.cnblogs.com/szitcast/p/11105899.html
整个流程就是用Qt打开源代码里Mysql的驱动项目,修改pro文件,编译(解决编译过程中的报错,主要是路径的错误),然后就可以得到dll,放到相应的位置,再放入libmysql.dll就可以了
这里把我的经验写下来
我的Mysql是zip解压缩后,自己创建my.ini并配置环境变量,安装路径是D:\services\MySQL\mysql-5.7.23-winx64\
我的QT是5.13版本,默认安装路径C:\Qt\Qt5.13.0
1 安装的时候模块要加入source
2 用QT打开源码的mysql项目,默认位置如下
C:\Qt\Qt5.13.0\5.13.0\Src\qtbase\src\plugins\sqldrivers\mysql\mysql.pro
3 修改mysql.pro,有三个修改的地方
include($$shadowed($$PWD)/configure.pri)
TARGET = qsqlmysql
HEADERS += $$PWD/qsql_mysql_p.h
SOURCES += $$PWD/qsql_mysql.cpp $$PWD/main.cpp
#修改1 Library 'mysql' is not defined.
#QMAKE_USE += mysql
OTHER_FILES += mysql.json
PLUGIN_CLASS_NAME = QMYSQLDriverPlugin
#修改2 添加以下三行
win32:LIBS += -LD:\services\MySQL\mysql-5.7.23-winx64\lib -llibmysql
INCLUDEPATH += D:\services\MySQL\mysql-5.7.23-winx64\include
DEPENDPATH += D:\services\MySQL\mysql-5.7.23-winx64\include
#修改3 错误提示 Cannot read C:/qtsqldrivers-config.pri: No such file or directory 改为绝对路径
#include(../qsqldriverbase.pri)
include(C:\Qt\Qt5.13.0\5.13.0\Src\qtbase\src\plugins\sqldrivers/qsqldriverbase.pri)
4 点击左下角的工具图标,Build项目,如果控制台没有报错的话就成功了,如果报错的话把构建的资料夹删除,看错误信息修改后,再从新构建
会在QT的安装目录的磁盘下新建\plugins\sqldrivers的文件夹
C:\plugins\sqldrivers
里面就有4个档案
qsqlmysql.dll,qsqlmysqld.dll
libqsqlmysql.a
libqsqlmysqld.a
5 把刚刚生成的 qsqlmysql.dll,qsqlmysqld.dll 拷贝到C:\Qt\Qt5.13.0\5.13.0\mingw73_64\plugins\sqldrivers
6 拷贝Mysql的libmysql.dll到QT的bin目录下, 例如我的是 D:\services\MySQL\mysql-5.7.23-winx64\lib的libmysql.dll拷贝到C:\Qt\Qt5.13.0\5.13.0\mingw73_64\bin
7 要使用mysql的项目pro文件要加入 QT += sql
=====20190911
这里补充下安装成功后测试的代码
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
#include
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
void connectDB();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::connectDB()
{
QSqlDatabase database;
database = QSqlDatabase::addDatabase("QMYSQL");
database.setHostName("localhost");
database.setDatabaseName("data");
database.setUserName("root");
database.setPassword("root");
database.setPort(3306);
bool ok = database.open();
if(ok){
qDebug()<<"成功连接数据库";
}else{
qDebug()<<"警告 无法连接数据库";
}
}
main.cpp
#include "mainwindow.h"
#include
#include
#include
#include "mymysql.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
w.connectDB();
QSqlQuery query;
query.exec("select * from student");
while(query.next())
{
qDebug()
<<"查询结果"
<
pro文件要加入sql
#-------------------------------------------------
#
# Project created by QtCreator 2019-08-23T13:20:25
#
#-------------------------------------------------
QT += core gui sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = MySqlSetup
TEMPLATE = app
#修改2
#QMAKE_LFLAGS +=D:\services\MySQL\mysql-5.7.23-winx64\lib\libmysql.dll
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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 you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
CONFIG += c++11
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
在数据库里新建一张表取名student ,栏位name,age 随便写一条数据,执行结果
10:32:00: Starting D:\workspaceForQt\build-MySqlSetup-Desktop_Qt_5_13_0_MinGW_64_bit-Debug\debug\MySqlSetup.exe ..
成功连接数据库
查询结果 "yiqian" "39"