Qt compiles mysql and creates a table before importing it. qtmysql

Source: Internet
Author: User

Qt compiles mysql and creates a table before importing it. qtmysql
In view of the fact that many students are always unable to compile myql for Qt and encounter various problems, I would like to write this tutorial to help students who need it.
First, you need to understand the purpose and principle of compilation.

Purpose: Qt 5.2 was earlier versions without the mysql driver. Therefore, you need to compile the mysql database driver. It can only be loaded by Qt after compilation. If you have installed a version later than Qt5.2, you don't need it because in later versions, Qt officially added this module.

You can check whether your driver already exists in the installation plug-in directory of Qt. For example, in my directory:

C: \ Qt \ 4.8.2 \ plugins \ sqldrivers. The current database driver is displayed.

It usually takes two.

Principle: First read the command: qmake "INCLUDEPATH + = C: \ mysql \ include" "LIBS + = C: \ mysql \ lib \ libmysql. lib" mysql. pro

Explanation: qmake --- is a compilation command of Qt China. It is often used for programming in linux. For details, refer to Baidu; INCLUDEPATH ---- The following paths are "include" and "lib" directories in the database. Why copy the two files to another disk? That is because mysql has spaces in the default installation path during installation, which cannot be passed during compilation. If not, you can try it. If not, you can test it on another disk; last mysql. pro --- this is the Qt project file. After each Qt project is created,. the file at the end of pro. When you open the Qt project later, double-click the file you want to open. the file at the end of pro will open the whole project.

Maybe I didn't explain the meaning clearly.

The specific process is described below:

Careful students will find that this problem occurs during qmake. Self-Compilation:

'Qmake' is not an internal or external command, or a program or batch file that can be run.

Cause: No environment variables are configured for Qt.

Solution: Find the installation directory of Qt. Here is my directory:

Find this in this directory and see qmake.


If not, right-click the desktop Qt program and open the file path:

Right-click my computer-click Properties-advanced system settings-Environment Variable-find path item-Edit

 

At this point, several other directory pages are configured as follows. We will not repeat it here.

Of course, the simplest and most direct is that configuration is not required. Compile with the command provided by Qt. Open the Start menu and find the Qt comman form. For example, I am using this:


Go to the plug-in directory-driver directory-database directory under the Qt source code directory. Note that the source code is compiled. Enter the directory line to start the introduction --- press Enter. After that, many people said they didn't respond. That's right. The purpose of this step is to prepare for the next compilation and generate the makefile file.

Because the mingw compiler is used, the mingw32-make command is used for compiling. If you are using vs, nmake is used for compiling.

Directly enter the command mingw32-make ---- press Enter. If there is no error, this process takes dozens of seconds. Then install: Enter the command: mingw32-make install. The generated plug-in can be admitted to the corresponding Qt directory. You can check whether the driver directory has two files, qsqlmysql. dll and libqsqlmysql4.a, which will be available after compilation.

At this time, you will find that the mysql driver is not found. The boot file is missing. In a document

The two files you copied earlier have a libmysql. dll file under lib.

Copy it to the startup directory of Qt. As with the previous method, right-click the Qt icon on the desktop, open the file location, and paste it in.

So far, the mysql compilation task has been completed.

So how can we know if our configuration is successful? The simplest thing is to write a simple program for detection.

Create a QtGui Qpplication project. Note that the SQL module is included (if not included, you can add it from the project after the project is created. Do not worry ). Here is a simple test program:

Header file to be added: # include <QSqlDatabase> # include <QDebug>

    qDebug() << "Available drivers:";    QStringList drivers = QSqlDatabase::drivers();    foreach(QString driver, drivers)        qDebug() << "\t "<< driver;    qDebug() << "End";
Write the above program to the main function.

If the configuration is successful, the following information is output in the window:

See QMYSQL above. It indicates that the mysql database driver has been detected. Now you can perform subsequent operations.

First, to connect to the database, you must create a database and a data table. How can you create a database? This principle is also the same as that of SQL Server, but it is a little different. It may be that the command is not familiar to everyone. You can create a text file and edit it as needed: Create a table using SQL statements, create a simple student table. Enter the SQL statement:

Note that after the input is complete, save the file. The file suffix is. SQL !!!!!

The next step is to import data tables.

Follow the command above: show databses; --- press enter to view the current database name. Next, create a database. This is the same as what we learned in sqlserver,

Create databses mydatabse;

Run the import command to import the created data table to the database ---

This completes the basic task. Here we only create a simple table. You can create a data table as needed.

Note: you are not allowed to use the database before executing the source command. That is to say, you must specify the database for which you want to import tables.

After that, insert data in the same way, write the insert Data command in a net text file, and then execute the import command.

After completing the preceding steps, you can perform a simple database connection test to check whether the database 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 (); // create a database connection if (! OK) {QMessageBox: critical (0, QObject: tr ("failed to connect to the database !!! "), Db. lastError (). text (); return false;} else {QMessageBox: information (0, QObject: tr ("Tips"), QObject: tr ("connected to the database successful !!! "); 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.exe c ();}
Click Run to see:

You can view the queried data.

So far, all the simple compilation and testing work has been completed. Dear friends, please try your Qt and mysql.

I have uploaded the test project file. You can download the source code.

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

The above content is purely personal understanding. If you have any questions, you are welcome to point out and learn together. Creaazylq@163.com)



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.