In view of the many students on the QT compilation myql is always unsuccessful, there are various problems, today we write this tutorial, I hope to help the students who need.
First, you need to understand the purpose and principle of compilation.
Objective: The QT 5.2 version was not MySQL-driven before, so it is necessary to compile MySQL database driver, only after the compilation is completed before it can be loaded by Qt. If you install a later version of Qt5.2, it is not necessary, because in later versions, QT officially added this module.
You can see if your driver already exists in Qt's install plugin directory. For example my in the directory:
C:\Qt\4.8.2\plugins\sqldrivers. You can see the current database driver
It usually takes two of them.
Principle: First Look at the command: Qmake "Includepath+=c:\mysql\include" "Libs+=c:\mysql\lib\libmysql.lib" Mysql.pro
Explanation: Qmake---is a compilation command of QT China, which is often used to make commands when programming under Linux, the details can be Baidu; Includepath----The following path is the directory of "include" and "Lib" in the database. Why copy these two files to a different disk? That is because MySQL in the installation of the default installation path when there are spaces, which can not be passed during the compilation process, the students do not believe it, not the words in the test to the other plate inside The last Mysql.pro---this is the QT project file, each QT project will be built after the completion of a. Pro file, the next time you open the QT project, double-click the file you want to open the end of the. Pro, the entire project is open.
Perhaps the above explanation doesn't explain what it means.
Let's talk about the specific process:
The careful classmate will find, qmake. This problem occurs when you compile yourself:
' Qmake ' is not an internal or external command, or a program or batch file that can be run
Cause: No environment variables for QT configuration
workaround : find the QT installation directory, my directory is here:
Find this thing in this directory, see it, qmake .
If you can't find it, you can do this: click the desktop QT Program right button to open the file path:
Right-click to open My Computer----Click Properties---Advanced system settings-----Environment variables----Locate the path entry---edit
At this point, several other directory pages are configured as such. There is no repetition here.
The simplest and most straightforward is, of course, without configuration. Compile directly with the command that comes with Qt. Open the Start menu and locate the QT comman form. Mine, for example, is this:
into the QT source directory of the plug-in directory--Drive directory---database directory. Note that this compilation is the source code. Then enter which row of directories you are about to start---press ENTER. After a lot of students say no response, that's right, this step is to prepare for the next compilation, will generate makefile files.
Because the MinGW compiler is used, compile with the Mingw32-make command, and if you are using VS, use NMAKE to compile it.
Direct Input Command mingw32-make----carriage return. If there are no errors, this process takes about 10 seconds. Then install: Enter the command: Mingw32-make install. You can test the generated plugin into a directory corresponding to QT. You can see if the driver directory has the Qsqlmysql.dll and libqsqlmysql4.a two files, the general compilation will be successful.
At this point the program will find that the MySQL driver is still not found. The boot file is missing. In the examination of a file
That's the two files you copied, there's a Libmysql.dll file under Lib.
Copy it to the start directory of Qt. Just like the previous open method, right-click the QT icon in the desktop mouse, open the file location, and then paste it in.
At this point, the MySQL compilation task is complete.
How to know if your configuration is successful, the simplest is to write a simple program detection.
Establish a Qtgui qpplication project. Note include the SQL module (if it is not included, you can add it from the project after you have finished building it, don't worry). is a simple test program:
Header files to add:#include<QSqlDatabase> #include <QDebug>
Qdebug () << "Available drivers:"; Qstringlist drivers = qsqldatabase::d rivers (); foreach (QString driver, drivers) qdebug () << "\ t" << driver; Qdebug () << "End";
Write the above program in the main function.
If the configuration is successful, the message will be printed below the window:
See the above qmysql, indicating that the MySQL database driver has been detected. Now you can do the following.
First, in order to connect the database, you must create a database and a data table, how to create it? This principle is also familiar with the SQL Server, but, a little bit different, may be to command everyone is not familiar with, arbitrarily create a new text file, for editing: Create a table with SQL statements, create a simple student table. Enter the SQL statement:
Note When the input is complete, save the file, and the file suffix is. SQL!!!!!
The next step is to import the data table when it is critical.
Follow the above command: show databses;---Enter to see the current database name. Next, create the database--as we did in SQL Server secondary School,
Create Databses Mydatabse;
Execute the Import command to import the previously written creation data table into the database---
This time, complete the basic task, here just create a simple table, you can according to your own needs to create a data table.
Note: You are not allowed to use the database until the source command is executed, that is, you must specify which database you want to import the table to.
When you are done, insert the data in the same way, and also write the Insert Data command in a Web text file, and then execute the import command.
Once the above steps are complete, a simple connection database test can be performed to detect whether the connection is possible.
#include <QApplication> #include "widget.h" #include <QMessageBox> #include <QSqlError> #include < Qtextcodec>bool Creatconnect () {Qsqldatabase db = Qsqldatabase::adddatabase ("Qmysql"); Db.sethostname ("127.0.0.1"); Db.setport (3306); Db.setdatabasename ("Student"); Db.setusername ("root"); Db.setpassword ("123456"); bool OK = Db.open ();//Establish database connection if (!ok) {qmessagebox::critical (0,QOBJECT::TR ("Connection database failed!!! "), Db.lasterror (). text ()); return false; } else {qmessagebox::information (0,qobject::tr ("Tips"), QOBJECT::TR ("Connection database success!!!") ")); return true; }}int Main (int argc, char *argv[]) {qapplication A (argc, argv); Qtextcodec *codec = Qtextcodec::codecforname ("GB2312"); Qtextcodec::setcodecforlocale (codec); Qtextcodec::setcodecforcstrings (codec); QTEXTCODEC::SETCODECFORTR (codec); if (!creatconnect ()) {return 1; } Widget W; W.show (); return a.exec ();}
Click Run To see:
You can see the data for the query.
At this point, all the simple compile test work is done. Let's try your qt and MySQL, little friends.
Test engineering Documents I have uploaded, we can download the source code.
http://download.csdn.net/detail/u013704336/8278693
The above content, purely personal understanding, if there are any problems, you are welcome to point out, learn together. ([email protected])
QT compiles MySQL and makes the import operation after creating the table