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)