QT 5 database operation (MySQL)

Source: Internet
Author: User

In fact, we all know that QT5 above are brought with the database driver, so, basically can be directly used, so if you want to know how to connect the database, please refer to the great God wrote. http://qtdebug.com/DB-AccessMySQL.html

About database operations

Qsqldatabase db = Qsqldatabase::adddatabase ("Qmysql"); Blog for the use of MySQL

Db.sethostname ("127.0.0.1"); Native Address

Db.setdatabasename ("Qt"); I created a database called Qt in MySQL.

Db.setusername ("root"); Account

Db.setpassword ("59826****"); Password

if (!db.open ())

{

Qdebug () << "failed to open database";

}

Qdebug () << "connected successfully";

About the database displayed in the table

Qtableview *_view = new Qtableview;

Qsqltablemodel *_model = new Qsqltablemodel;

Qvboxlayout *lay = new Qvboxlayout (this);

_model->settable ("user"); Table names in the database

_model->selection (); Select all the data in the table

_view->setmodel (_model); Pass _model to _view.

Lay->addwidget (_view); Add a space to the qvboxlayout and show

Continuous update, not to be continued

This article belongs to author original, if need reprint please specify http://i.cnblogs.com/EditPosts.aspx?opt=1

QT 5 database operation (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.