QT5 connecting MySQL

Source: Internet
Author: User

Summary

The default support for MySQL database is already provided in Qt 5 , and if you want to use the database, you need to install the database first, here we introduce MySQL in the Windows system Database installation and ease of use.

How does QT remotely connect to a database using MySQL?

The code is as follows:

Qdebug () <<qsqldatabase::drivers();

Qdebug () <<qcoreapplication::librarypaths();

Qsqldatabasedb=qsqldatabase::adddatabase("Qmysql");

db. SetHostName ("10.127.1.13");

db. Setusername ("root");

db. SetPassword ("Redhat");

db. Setdatabasename ("test");

BOOLbRet=db. Open();

If(bRet= =false)

{

Qdebug () << "errorOEPNdatabase"<<db. LastError(). text ();

Exit (0);

}

Qdebug ()<<"opendatabaseSuccess";

The following questions can be found:

MySQL driver failed to load, how to solve?

Download the driver for the connection

https://dev.mysql.com/downloads/connector/c/

The installation location defaults to

C:\Program Files (x86) \mysql\mysql Connector C 6.1\lib

Copy the Libmysql.dll from the MySQL installation directory to the bin directory under the QT installation directory

Run again

QT5 connecting MySQL

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.