当前位置:网站首页>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 ";
    }

 

 

原网站

版权声明
本文为[cathy18c]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280534095144.html