QT compiles MySQL and makes the import operation after creating the table

Source: Internet
Author: User
Tags mysql in

in view of many classmates to QT compiles myql always cannot succeed. There are a variety of problems, today we write this tutorial, hoping to help the students who need.
First, the purpose and principle of the compilation need to be clarified.

Purpose: The Qt 5.2 version number was used without MySQL driver. So it is necessary to compile MySQL database driver, only after compiling the ability to be loaded QT. Assuming that you installed the Qt5.2 later version number, it is not necessary, because in the later version number, the official QT added this module.

Be able to see if your driver already exists under Qt's Install Plugins folder.

For example, my in folder:

C:\Qt\4.8.2\plugins\sqldrivers. To see the current database driver

Generally need two.

Principle: First Look at the command: Qmake "Includepath+=c:\mysql\include" "Libs+=c:\mysql\lib\libmysql.lib" Mysql.pro

Explanation: Qmake---is a compile command for QT China, which is often used to make commands for programming under Linux. Details can be Baidu; Includepath----The following path is the folder for "include" and "Lib" in the database.

Why would you want to copy the two files to a different disk? That is due to MySQL in the installation of the default installation path has spaces, which is not in the compilation process, the students can try to do not believe, not the words in the test to the other plate inside The last Mysql.pro---this is the QT project file, each qtproject will generate a file ending with. Pro, and then double-click the file you want to open when you open the Qtproject. The entire project is open.

Perhaps the above explanation doesn't explain what it means.

The following is a detailed procedure:

The careful classmate will find, qmake. This problem occurs when you compile yourself:

' Qmake ' is not an internal or external command, nor is it an executable program or batch file

Cause: No environment variables for QT configuration

workaround : Find the qt installation folder, my folder is here:

Find this thing under this folder, see it qmake


Can not find the words to 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 folder pages are configured like this. There is no more 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 find the QT comman window.

For example mine is this:


Go to the Plugins folder under the Source code folder of QT--Drive folder---database folder. Note that this compilation is the source code. then enter which line of folders to start with---press ENTER.

After a lot of classmates said no response, that's right, this step is to prepare for the next compilation, will generate makefile files.

Since the MinGW compiler is used, compile with the Mingw32-make command, assuming you are using VS, then use NMAKE to compile.

Direct Input Command mingw32-make----carriage return.

Assume there are no errors. This process takes about 10 seconds. Then install: Enter the command: Mingw32-make install. Ability to test the generated plugin into the appropriate QT folder.

You can see if the driver folder has the Qsqlmysql.dll and libqsqlmysql4.a two files, the general compilation will be successful.

At this point the code 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 earlier. There's a libmysql.dll file under Lib.

Copy it to Qt's Startup folder. Just like the previous open method, right-click the QT icon in the desktop mouse, open the file location, and then paste it in.

So far. The MySQL compilation task is complete.

How do you know if your configuration is successful, the simplest thing is to write a simple program to check.

Establish a Qtgui qpplicationproject. Note include the SQL module (assuming it is not included, you can add it from project after you have finished building it, and don't worry about it). is a simple test procedure:

Header files to include:#include<QSqlDatabase> #include <QDebug>

    Qdebug () << "Available drivers:";    Qstringlist drivers = qsqldatabase::d rivers ();    foreach (QString driver, drivers)        qdebug () << "\ t" << driver;    Qdebug () << "End";
Write the above procedure in the main function.

If the configuration is successful, the message will be printed below the form:

See the Qmysql above, indicating that the MySQL database driver has been detected. It is now possible to do the following.

First, in order to connect to the database. You must create a database and a data table, how do you create it? This principle is also familiar with the SQL Server, just, slightly different, it may be the command people are not familiar with, feel free to create a text file, to edit: The SQL statement created by the table, create a simple student table.

Enter the SQL statement:

Note Save the file when you are finished typing. The suffix of the file is. sql!!!

。。

The next step is to import the data table when it is critical.

Follow the above command: show databses;---enter. You can see the current database name. Next, create the database--as we did in SQL Server secondary School.

Create Databses Mydatabse;

Run the import command. Import the Created data table that you wrote earlier into the database---

This time, finished the main task, here is simply to create a simple table, can be based on your own need to create a data table.

Note: You are not allowed to use the database until you run the source command. That is, you must specify which database you want to import tables for.

After the same method, insert the data, and also write the Insert Data command in a Web text file, and then run the import command.

After the above steps are complete. A simple connection to the database can be performed to test if it can be connected.

#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 ("Connect database successfully ...!

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

Can see the data of the query.

At this point, all the simple compilation test work has been completed.

Let's try your qt and MySQL, little friends.

Test project files I have uploaded, you can download the source code.

http://download.csdn.net/detail/u013704336/8278693

The above content. Purely personal understanding, if there is no matter what the problem, you are welcome to point out, learn together. ([email protected])



QT compiles MySQL and makes the import operation after creating the table

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.