Summary
The default support for MySQL database is already provided in Qt 5 , and if you want to use the database, you need to install the database first, here we introduce MySQL in the Windows system Database installation and ease of use.
How does QT remotely connect to a database using MySQL?
The code is as follows:
Qdebug () <<qsqldatabase::drivers();
Qdebug () <<qcoreapplication::librarypaths();
Qsqldatabasedb=qsqldatabase::adddatabase("Qmysql");
db. SetHostName ("10.127.1.13");
db. Setusername ("root");
db. SetPassword ("Redhat");
db. Setdatabasename ("test");
BOOLbRet=db. Open();
If(bRet= =false)
{
Qdebug () << "errorOEPNdatabase"<<db. LastError(). text ();
Exit (0);
}
Qdebug ()<<"opendatabaseSuccess";
The following questions can be found:
MySQL driver failed to load, how to solve?
Download the driver for the connection
https://dev.mysql.com/downloads/connector/c/
The installation location defaults to
C:\Program Files (x86) \mysql\mysql Connector C 6.1\lib
Copy the Libmysql.dll from the MySQL installation directory to the bin directory under the QT installation directory
Run again
QT5 connecting MySQL