1024programmer Mysql How to connect to mysql in qt5.8

How to connect to mysql in qt5.8

How to connect to mysql with qt5.8: first compile and generate the mysql driver; then download the “libmysql.dll” library and put it in the Qt bin folder; finally create a connection code file for testing Can.

Recommendation: “Mysql Video Tutorial”

qt5.8 connects to mysql database

Qt Creator. Take qt5.8.0 and mysql5.7.18 as examples below.

Preparation

1.qt5.2 and above, there is a mysql driver by default, we can in (eg: D:\Qt\Qt5.8.0\5.8\mingw53_32\ plugins\sqldrivers) to find the mysql driver. If there is no driver, then you have to compile and generate it yourself.

2. Download mysql (qt cannot find the library file of mysql) The file name of the library is “libmysql.dll”, and this library file should be placed in the bin folder of Qt (for example: D:\ Qt\Qt5.8.0\5.8\mingw53_32\bin) This file is usually in the lib directory of mysql.

Test code

#include "mainwindow.h"
 #include 
 #include 
 #include 
 #include 
 #include 
 #include 
 int main(int argc, char *argv[])
 {
     QApplication a(argc, argv);
     MainWindow w;
     w. show();
      QStringList drivers = QSqlDatabase::drivers();
      qDebug() <<drivers;
      QSqlDatabase dbconn;
      QSqlQuery query;
      dbcOnn= QSqlDatabase::addDatabase("QMYSQL");
      dbconn.setHostName("192.168.80.221");//host name
      dbconn.setDatabaseName("iacrms");//database name
      dbconn.open("gdzt", "039.com");//username, password, return 1 if successful
      query = (QSqlQuery)dbconn;
      query.exec("select * from tb_UserQQInfo");
      while(query. next())
      {
          int id = query. value(0). toInt();
          QString name = query. value(1). toString();
          qDebug() <<QString::number(id) + " : "+ name;
      }
     return a.exec();
 }

Notes

1. The qt version must be the same as that of mysql. If it is 64-bit, it must be 64-bit, or both are 32-bit.

The above is the detailed content of how qt5.8 connects to mysql. For more information, please pay attention to other related articles on 1024programmer.com!

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/how-to-connect-to-mysql-in-qt5-8/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索