Explain the link of the Qt database and how to use SQL

Source: Internet
Author: User
Tags how to use sql

Qt DatabaseLink and how to useSQLIs the content to be introduced in this article, mainly to learnQTMediumDatabaseAndSQLFor more information about how to use it, see this article.

I have never been in touch with the database. I suddenly had the opportunity to use MySQL for the first time. I just learned SQL syntax and learned how to use Qt in combination with the database, by the way, I tried to connect the database to Qt and use SQL. I won't talk about the SQL part, and Qt will talk about the link part first.

1. database-driven first

Now, Qt SDk for release converts LibMySQL. lib in MySQL/opt to. a. You can skip this step by using MSVC ). Then go to qt/src/plugins/sqldriver/mysql to compile the driver.

 
 
  1. QMAKE -o Makefile "INCLUDEPATH+=MYSQL/INCLUDE" "LIBS+=MYSQL/LIB/OPT/libmysql.a" mysql.pro 

Pay attention to the qmake parameter and make it again. This part is described in detail on the Internet,

2. Use SQL in Qt

Mainly the following classes

QSqlDatabase is built on the Database Link
 
QSqlQuery is used to execute SQL statements.
 
QSqlTableModel combined with QTableView can output database tables

Paste the simple Demo I wrote

 
 
  1. QSqlDatabase db = QSqlDatabase: addDatabase ("QMYSQL"/* "QODBC" */); // becomes the new default connection
  2. Db. setUserName ("root"); // User Name
  3. Db. setPassword ("password"); // password
  4. Db. setHostName ("localhost ");
  5. Db. setDatabaseName ("test"); // Database Name
  6. Db. setConnectOptions ("CLIENT_SSL = 1; CLIENT_IGNORE_SPACE = 1"); // use SSL
  7. Db. setPort (3306); // Port
  8. If (db. open ()){
  9. QDebug () <"open/n" <db. lastError (). driverText () <"/n ";
  10. }
  11. Else {
  12. QDebug () <"open faile/n ";
  13. }
  14. QSqlQuery query; // used to execute the SQL language
  15. Query.exe c ("show databases"); // very convenient
  16. While (query. next ()){
  17. QDebug () <query. value (0). toString () <"/n ";
  18. }
  19.  
  20. QSqlTableModel * model = new QSqlTableModel; // indirectly load the database table into QTableView
  21. Model-> setTable ("people"); // table name
  22. Model-> setEditStrategy (QSqlTableModel: OnManualSubmit );
  23. Model-> select ();
  24. // Model-> removeColumn (0); // don't show the ID
  25. // Model-> setHeaderData (0, Qt: Horizontal, QObject: tr ("ID "));
  26. Model-> setHeaderData (0, Qt: Horizontal, tr ("Name "));
  27. Model-> setHeaderData (1, Qt: Horizontal, tr ("Age "));
  28. Model-> setHeaderData (2, Qt: Horizontal, tr ("Sex "));
  29.  
  30. QTableView * view = new QTableView (this );
  31. View-> setModel (model );
  32. Db. close ();
  33. QGridLayout * gl = new QGridLayout ();
  34. Gl-> addWidget (view );
  35. This-> setLayout (gl );

Summary: DetailsQt DatabaseLink and how to useSQLI hope this article will help you!

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.