How to use MySQL and statically compile Qt in Qt How to add MySQL (2)

Source: Internet
Author: User
Tags mysql in mysql version sqlite

Following the continuation of the previous article:

The previous article said, "finally write a demo of my SQL Project discovery can run successfully, but could not find the database file created reason to find"

Note that the corresponding code for the above problem is as follows:

#include <QDebug> #include <QSqlDatabase> #include <QSqlQuery> #include "mainwindow.h" #include "ui_ Mainwindow.h "Mainwindow::mainwindow (Qwidget *parent): Qmainwindow (parent), UI (new Ui::mainwindow) {Ui->setupu    I (this);    Qsqldatabase db = Qsqldatabase::adddatabase ("Qmysql");    Db.setdatabasename ("my.db");    Db.sethostname ("localhost");    Db.setport (3306);    Db.setdatabasename ("Datatest");    Db.setusername ("root");    Db.setpassword ("123456");    if (!db.open ()) {qdebug () << "CREATE Database Failed";    } else {qdebug () << "CREATE Database Success";    } qsqlquery query (db);    Query.exec ("CREATE TABLE student (ID primariy key, name varchar (20))");    Query.exec ("INSERT into student values (1, ' Wangli ')");    Query.exec ("SELECT * from student"); while (Query.next ()) {qdebug () << "ID:" <<query.value (0). ToInt () << "Name:" <<query.value (1). t    Ostring (); }}mainwindow::~mainwindow () {Delete UI;} 

 

The cause of the problem is:

Query.exec ("CREATE TABLE student (ID primariy key, name varchar (20))");
This line of code is now correct
The spelling of the previous error is
Query.exec ("CREATE TABLE student (ID primariy key, name Vchar (20))");



This small problem, led me to think that the company's computer Configuration QT development environment can not use the MySQL database, resulting in my whole tangled two days.
Here is a record of my analysis of this problem using MySQL in QT:
<1> first I wrote a demo listing all the SQL drivers supported in the qt5.5.0 version of the installation, and found that there was MySQL in it, thinking that you could use MySQL as you would with SQLite.
<2> actually follow the online operation, first download MySQL and then can't wait to write code verification found still can't use the debug directory without xxx.db file generation, tangled for a long time, finally found that MySQL is no xxx.db file
<3> then manually compiled the MySQL plugin n times, I have been "debug directory whether there are xxx.db files" as MySQL is ok this is wrong, continue to say
<4> I suspect is the download of the MySQL version of the problem, the bit problem, the installation path has spaces and the Chinese problem QT corresponds to the version and number of questions, eventually also read the blog, someone proposed QT 5.5 MySQL source has problems, eventually no results
<5> end up using the same QT version of the home computer as the Win7 system, as follows

Here's a little summary:
The way to use MySQL database in &LT;1&GT;QT is: qt older version need to manually compile the MySQL plugin to see my link in the previous blog
<2> I now use QT is 5.5.0 can directly use the MySQL database, the direct word is "to copy the MySQL installation directory libmysql.dll files in the QT installation directory in the bin directory will be"
My computer is to be D:\mysql\lib\libmysql.dll note here D:\mysql directory is a MySQL installed directory copy to C:\Qt\Qt5.5.0\5.5\mingw492_32\bin down
<3> because Qt is in the string to write SQL statements, many times, the SQL statement error will not have obvious hints, so only in the terminal to test their own written database statements no problem, can be sent to the code, or the problem is difficult to trace






Here's a look at the detailed procedures for using MySQL:
<1> download a MySQL version from MySQL website
Specifically, community->downloads-> Select a version I chose the following

Download the installation on the D disk to create a MySQL directory click on the MSI file, install it into the created directory, here set the password for 123456 port to keep the default 3306 unchanged installed the following


Then copy the Libmysql.dll below Lib to the C:\Qt\Qt5.5.0\5.5\mingw492_32\bin directory



OK, then you can use the MySQL database in Qt.

<2> Usage Examples:
Add the MySQL path to the environment variable D:\mysql\bin Add environment variable run terminal input mysql-u root-p Enter the password 123456 the following






The code is the code used in the project above:
<1> first login to MySQL with a command line the name is datatest.
The <2> code connects to this database and creates a table student and inserts a record code to query this record is OK
<3> Check this record on the command line is OK, see above
<4> I thought in the QT Project compile directory, also generates a datatest this database file name result a look at nothing

No, No. (In my experience with SQLite, this directory should not have a datatest file?).  where did the MySQL database file go? )   
Execute Show variables like '%datadir% '; Found the path in



Using MySQL to do this, note that this is only a dynamic use of static compiled QT program? How to use MySQL in a static compilation next story


How to use MySQL and statically compile Qt in Qt How to add MySQL (2)

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.