Recently in the graduation design, a lot of students choose to use QT and MySQL database program. However, these two development tools are not a perfect combination to use. Qt usually comes with a MySQL driver, but there is not much that can be used directly. Most blog papers give the MySQL-driven compilation process. Here is a ready-made, already compiled drive. Available in Win10, Qt5.6, and mysql5.5 versions.
Files required to connect to the database:
Link: Http://pan.baidu.com/s/1bpN6NjL Password: 86ys
After installing QT and MySQL, configure the environment variables. Use the following program to test if you can connect
Add in Conmysql.pro
1 QT + = sql
Conmysql.pro
1 #ifndef Dbhelper_h2 #defineDbhelper_h3 4#include <QSqlDatabase>5#include <QSqlQuery>6#include <QObject>7#include <QtDebug>8#include <QString>9#include <QXmlStreamReader>Ten One classDBHelper A { - Public: - Staticdbhelper*Getdbhelper (); the BOOLOpen (); - voidClose (); - - voidParseblog (); + Private: - DBHelper (); + StaticDBHelper *_dbhelper; A at QString _hostname; - int_port; - QString _databasename; - QString _username; - QString _password; - in qsqldatabase Dbsql; - }; to + #endif //Dbhelper_hDBHelper
1#include"Dbhelper.h"2 3 dbhelper::D bhelper ()4 {5_hostname ="127.0.0.1";6_port =3306;7_databasename ="Login";8_username ="Root";9_password ="Xiaoqi";Ten OneDbsql=qsqldatabase::adddatabase ("Qmysql"); A } - - BOOLDbhelper::open () the { - Dbsql.sethostname (_hostname); - Dbsql.setport (_port); - Dbsql.setdatabasename (_databasename); + Dbsql.setusername (_username); - Dbsql.setpassword (_password); + if( !Dbsql.open ()) A { at return false; - } - return true; - } - - voiddbhelper::close () in { - if(Dbsql.isopen ()) to { + dbsql.close (); - } the } * $ Panax Notoginsengdbhelper* Dbhelper::_dbhelper =NULL; - theDBHelper *Dbhelper::getdbhelper () + { A if(Dbhelper::_dbhelper = =NULL) the { +Dbhelper::_dbhelper =NewDBHelper (); - } $ return_dbhelper; $}Dbhelper.cpp
Add in Logical file
dbhelper* db = dbhelper::getdbhelper (); if (!db->Open ()) { " database load failed! "} " ; }
If you are prompted qmysql driver not loaded , you will need to replace the source file with the files in the above network disk
1. Replace the original file in the X:\Qt\Qt5.6.0\5.6\mingw49_32\plugins\sqldrivers with Sqldriver.rar decompression
2. Place the libmysql.dll in the MySQL path or the libmysql.dll of the network into the Windows path of the C drive, in the debug program
If the above red lettering is not indicated, the driver loading is successful and the corresponding connection parameters are modified according to the MySQL parameters.
DBHelper class Explanation:
(1) Initialize the connection string in the constructor, where you can use XML to save the connection parameters, initialization is to read the XML file, easy to modify the content
(2) The Getdbhelper function can obtain an instance of the class. Using singleton mode, the class is initialized only once in the program, and each time you can get to this instance by this method
(3) Open and close functions
Complete logic code is attached
1 voidmainwindow::on_pushbutton_clicked ()2 {3dbhelper* db = Dbhelper::getdbhelper ();//Get Instance4 if(!db->Open ())5 {6Qdebug () <<"Database loading failed!";7 }8 //Logic Code9QString id = ui->lineedit->text ();TenQString pass = ui->lineedit_2->text (); One intN; A qsqlquery query; -Query.prepare ("Select COUNT (*) from logintable where ID =? and password =?"); -Query.bindvalue (0, id); theQuery.bindvalue (1, pass); - query.exec (); - while(Query.next ()) - { +n = Query.value (0). ToInt (); - } + if(n = =1) AQdebug () <<"Landing Success! "; at Else -Qdebug () <<"Login failed! "; - -Db->close ();//Close the database -}Login.cpp
Connect to MySQL using Qt5.6