当前位置:网站首页>QML connecting to MySQL database
QML connecting to MySQL database
2022-06-13 03:08:00 【cathy18c】
First contact qml, I don't know how to connect mysql database , Think like c# equally , You can connect with the database operation class , I didn't expect to compile it myself
Let's talk about my environment
Qt edition :Qt 5.15.2 Windows edition
Qt Compiler Version :mingw81_64
MySQL edition :mysql-5.7.29-winx64
I created a new project , stay main.cpp Of main A sentence is written in the method
// Print Qt Supported database drivers
qDebug() << QSqlDatabase::drivers(); Output is as follows :
("QSQLITE", "QODBC", "QODBC3", "QPSQL", "QPSQL7")
You can see that the default does not MySQL The driver !!
as a result of :Qt 5 In the higher version, the right to MySQL Database default support , This is because with the commercial version of MySQL Introduction ,MySQL It's no longer a fully open source database , and Qt By default, only fully open source databases will be supported , And the above databases are all open source . So how to give Qt 5.15 add to MySQL What about the driver ? After study , The solution is as follows :
First step :
First open MySQL Install under directory lib Folder , my lib The address of the folder is as follows :C:\Program Files (x86)\MySQL\mysql-5.7.29-winx64\lib
Open and choose libmysql.dll and libmysql.lib , Copy :

then , Paste it to Qt Of MinGW Compiler bin Folder Next , my bin The address of the folder is :
D:\Qt\5.15.2\mingw81_64\bin
You can choose the right location to copy according to your installation location :

The second step :
1. find Qt Installation directory sql In drive mysql Folder
This mysql Folder , I've been looking for it for a long time, but I can't find it , Later Baidu found , I installed it qt The source code was not installed Sources
And then I found it qt Installation path for , Find... In it MaintenanceTool.exe, Double-click to open . Do not choose to add or remove components first , Select the update component of the second item , And then put this MaintenanceTool Tool update , Then you can select the add component option , It is found that new components can be installed

This picture is borrowed from others , You just need to know that Sources Just install it
Once installed ,D:\Qt\5.15.2 There will be Src Folder
find D:\Qt\5.15.2\Src\qtbase\src\plugins\sqldrivers\mysql This folder , Double click on mysql.pro file

mysql.pro There are several changes in it :
1, Comment out QMAKE_USE += mysql
2, Add the code :
win32:LIBS += -L$$quote(C:\Program Files (x86)\MySQL\mysql-5.7.29-winx64\lib) -llibmysql
INCLUDEPATH += $$quote(C:\Program Files (x86)\MySQL\mysql-5.7.29-winx64/include)
DEPENDPATH += $$quote(C:\Program Files (x86)\MySQL\mysql-5.7.29-winx64/include)It's important to be careful here , A pit !!!
Because of my mysql The installation directory is in C:\Program Files (x86)\MySQL\mysql-5.7.29-winx64 This path has spaces , So want to use $$quote Wrap the path
Then build , It's the hammer in the lower left corner of the interface , The following error will appear :
Cannot read D:/qtsqldrivers-config.pri: No such file or directory
This problem occurs because there is no such file , But it can be in the directory D:\Qt\5.15.2\Src\qtbase\src\plugins\sqldrivers Find below configure.pri This file , Then we'll take the original need qtsqldrivers-config.pri Note out where you are , Change to configure.pri.

open qsqldriverbase.pri file , Comment out the fourth line , namely #include($$shadowed($$PWD)/qtsqldrivers-config.pri) , Then add include(./configure.pri), preservation , complete .
Next, rebuild , No report error .D:\plugins\sqldrivers Next generation 3 File

among ,qsqlmysql.dll and qsqlmysql.dll.debug That's what we need Qt Database driver file .
3. Copy the driver file to Qt Of sqldrivers In the folder
Select the one generated above qsqlmysql.dll and qsqlmysql.dll.debug Drive files and copy :
And then take it. Paste the Qt Install under directory sqldrivers Folder Next :D:\Qt\5.15.2\mingw81_64\plugins\sqldrivers
4. Be accomplished !
Now? , We have succeeded in giving Qt Added MySQL The driver !
Next restart Qt, To test , Continue to use the following code to print Qt Supported database drivers :
// Print Qt Supported database drivers
qDebug()<<QSqlDatabase::drivers();It can be found that there is already MySQL The driving force of !
("QSQLITE", "QMARIADB", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3", "QPSQL", "QPSQL7")
qDebug() << QSqlDatabase::drivers();
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName(" Database address localhost perhaps 192.x.x.x");
db.setDatabaseName(" Database name ");
db.setUserName("root");
db.setPassword("");
bool ok = db.open();
if (ok)
{
qDebug() << " Connect to database ok";
}
else
{
qDebug() << " Failed to connect to database ";
} 
边栏推荐
- Wechat applet switch style rewriting
- Introduction to facial expression recognition system -- offline environment configuration
- HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)
- Radium laser radar C16 data to PCD (based on ROS)
- 通过Web ETL统一调度和管理DataX任务
- Five old code farmers, program life review: peace of mind is not the place to go
- Professional database management software: Valentina Studio Pro for Mac
- CDN single page reference of indexbar index column in vant framework cannot be displayed normally
- C simple understanding - arrays and sets
- Typical application of ACL
猜你喜欢

JVM virtual machine stack (III)

C simple understanding - arrays and sets

Prometheus node_ Exporter installs and registers as a service

Linked lists: rearranging linked lists

Vscode liveserver use_ Liveserver startup debugging

Wechat applet switch style rewriting

Vs Code modify default terminal_ Modify the default terminal opened by vs Code

Vs 2022 new features_ What's new in visual studio2022

Few-shot Unsupervised Domain Adaptation with Image-to-Class Sparse Similarity Encoding

Es and kibana deployment and setup
随机推荐
MySQL index
C# . NET ASP. Net relationships and differences
When the flutter runs the project, the gradle download fails, and the running gradle task 'assemblydebug' is always displayed
Ijkplayer source code ---packetqueue
Scala implements workcount
CDN single page reference of indexbar index column in vant framework cannot be displayed normally
Exercise 8-3 rotate array right
Typical application of ACL
Capital digit to number format
Installing the IK word breaker
Hash table: the time complexity of insert, delete and random access is O (1)
Logiciel professionnel de gestion de base de données: Valentina Studio Pro pour Mac
. Net compact Framework2.0 wince intelligent device development project experience sharing Net drag space advanced
Rounding in JS
The extra money we made in those years
SQL execution process in MySQL (3)
Introduction to facial expression recognition system - Technical Paper Edition
C 10 new features_ C 10 new features
MySQL transactions and locks (V)
HEAP[xxx.exe]: Invalid address specified to RtlValidateHeap( 0xxxxxx, 0x000xx)