First knowledge of MySQL database

Source: Internet
Author: User
Void mainwindow: conect () {/* QT demos or exmples provides usage of SQLite databases. */Qsqldatabase DB = qsqldatabase: adddatabase ("qmysql"); // use MySQL to load the MySQL driver dB. sethostname ("localhost"); // set the host name. You must set the host name dB before connecting to the database. setdatabasename ("test"); // you can specify the database name dB. setusername ("root"); // username dB when logging on to MySQL. setpassword ("123"); // The bool OK password when logging on to MySQL = dB. open (); // open the database if (OK) {qdebug () <"Open Database! "<Endl;} else {qmessagebox: Critical (0, qobject: TR (" database error "), DB. lasterror (). text () ;}} void mainwindow: Execute () {qsqlquery query; // execute the SQL statement query.exe C ("CREATE TABLE employee (ID int (11) primary key, name varchar (50), description varchar (255) "); // create the query.exe C (" insert into employee values (1, 'anpan', 'student ') "); // insert data query.exe C (" insert into employee values (2, 'caoling', 'student ') "); query. Exec ("insert into employee values (3, 'lvsumei ', 'student')"); query.exe C ("insert into employee values (4, 'panjiaoli', 'student ') ");} void mainwindow: Display () {qsqltablemodel * model = new qsqltablemodel; // data model-> settable (" employee "); // set the table 'employee model-> seteditstrategy (qsqltablemodel: onmanualsubmit); // All changes will be cached in the model until submitall () or revertall () the model-> select (); // select () function is called to check the status of one or more interfaces. For each set of interfaces, the caller can query its readability, writability, and error status information model-> setheaderdata (0, QT: horizontal, qobject :: TR ("ID"); // set the header model-> setheaderdata (1, QT: horizontal, qobject: TR ("name ")); model-> setheaderdata (2, QT: horizontal, qobject: TR ("Description"); this-> UI-> tableview-> setmodel (model );}

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.