Introduction
mariadb is a branch of mysql, and the code is the same as calling mysql.
mariadb installation
Download mariadb64: https://downloads.mariadb.org/ .
qt compile mysql driver
Because qt5.12 does not have mysql driver, you need to compile it yourself.
Download the 64-bit MySQL Community Server: https://dev.mysql.com/downloads/mysql/
After downloading, unzip it, mine is in F:\mysql-8.0.20-winx64.
Open the mysql project with qt creator: D:\Qt\Qt5.12.6\5.12.6\Src\qtbase\src\plugins\sqldrivers\mysql (find your own qt installation path)
and add it to the .pro file
#该路径是下载解压后的mysql的头文件路径
INCLUDEPATH +="F:/mysql-8.0.20-winx64/include"
#这是刚才下载解压后的MySQL的库文件路径
LIBS +="F:/mysql-8.0.20-winx64/lib/libmysql.lib"
#文件生成路径
DESTDIR = ../mysql/lib/
Copy the generated qsqlmysql.dll and qsqlmysqld.dll to the D:\Qt\Qt5.12.6\5.12.6\mingw73_64\plugins\sqldrivers directory. The
compiled driver: https://download.csdn.net/download/sinat_33859977 /12403190
Copy libmariadb.dll
Find the installation directory of mariadb, copy libmariadb.dll under C:\Program Files\MariaDB 10.4\lib\ to D:\Qt\Qt5.12.6\5.12.6\mingw73_64\bin directory. Modify the name of libmariadb.dll to libmysql.dll.
test
Add in .pro
QT += sql
main.cpp
#include "widget.h"
#include <QApplication>
#include <QtDebug>
#include <QSqlDatabase>
#include <QSqlQuery>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
qDebug() << "Avaliable drivers:";
QStringList drivers = QSqlDatabase::drivers();
foreach (QString driver, drivers)
qDebug() << driver;
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("127.0.0.1");
db.setPort(3306);
db.setDatabaseName("test");
db.setUserName("root");
db.setPassword("123456");
if(db.open())
{
qDebug() << "open sucess";
}
else
{
qDebug() << "open faile";
}
return a.exec();
}
Output result:
Avaliable drivers:
"QSQLITE"
"QMYSQL"
"QMYSQL3"
"QODBC"
"QODBC3"
"QPSQL"
"QPSQL7"
open sucess