QT5 connects to Mysql

Summary

The default support for the MySQL database has been provided in Qt 5. To use the database, you need to install the database first. Here we introduce the installation and simple use of the MySQL database in the Windows system .

 

How does Qt use Mysql to connect to the database remotely?

 

code show as below:

    

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";

 

There will be the following problems:

Mysql driver failed to load, how to solve it?

Download the connected driver

https://dev.mysql.com/downloads/connector/c/

The installation location defaults to

C:\Program Files (x86)\MySQL\MySQL Connector C 6.1\lib

Copy libmysql.dll in the mysql installation directory to the bin directory in the qt installation directory.

run again

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325124085&siteId=291194637