A simple tutorial on Qt Databases

Source: Internet
Author: User

OT DatabaseIs the content to be introduced in this article, mainly to understandQTInDatabase.QtUsing the QtSql ModuleDatabaseThis module is a collection of classes. To use this module, we need to add the header file # include <QtSql>, and add a line of code to the project file:

 
 
  1. QT += sql 

QSqlDatabase

QSqlQuery is used to execute SQL-related statement transactionsDatabaseIs an important function, the so-called transaction is a user-defined database operation sequence, these operations are either full or all do not, is an inseparable unit of work. Start a transaction operation with transaction () in Qt and use commit ()

The function or rollback () function ends. Commit () indicates commit, that is, all operations to commit a transaction. Specifically, all the updates to the database in the transaction are written back to the database, and the rollback () after the transaction ends normally indicates rollback, that is, a certain fault occurs during the transaction operation, the transaction cannot continue. The system revokes all completed operations on the database in the transaction and rolls back to the State at the beginning of the transaction.

QSqlDatabase: database () returns the QSqlDatabase object generated before the program. The hasFeature () function can check whether a database supports transactions.

Qt uses its own mechanism to avoid using SQL statements. It provides us with a simpler database operation and data display model. They are read-only QSqlQueryModel, QSqlTableModel for a single table, and QSqlRelationalTableModel that supports foreign keys.

To make it readable and writable, You need to inherit from the QSqlQueryModel class and rewrite the setData () and flags () functions. If we want to change the display of data, we need to rewrite the data () function.

QSqlTableModel, which provides an editable data model that can read and write a single SQL table. Modifiable, inserted, deleted, queried, and sorted.

This model can save all modifications to the model first. Only after the modifications are submitted will the changes be actually written to the database. Of course, this is also because we set its storage policy at the beginning:

 
 
  1. model->setEditStrategy(QSqlTableModel::OnManualSubmit); 

OnManualSubmit indicates that we need to submit changes to make them take effect.

QSqlRelationalTableModel provides an editable data model for a single database table. It supports foreign keys.

The QSqlRelationalDelegate delegate class in Qt can be used to modify related tables. We only need to add a line of code:

 
 
  1. ui->tableView->setItemDelegate(new QSqlRelationalDelegate(ui->tableView));  
  2. QT += xml  

QtXml Module: It mainly serves two methods for operating XML documents: DOM and SAX. DomDocument Object Model (Document Object Model) converts an XML document into a tree structure that can be traversed by an application, so that nodes in the document can be randomly accessed. Its disadvantage is that it needs to read the entire XML file into the memory, which consumes a lot of memory.

In QDom, the entire XML file is read to the doc object in the memory. Then, use the node QDomNode) to operate the doc object, such as the XML description, elements, attributes, and text, which makes it easy to operate the XML document, we only need to convert the node to the corresponding type through the conversion function, as shown in figure

 
 
  1. QDomElement e = n.toElement(); 

If you only want to read and display the entire XML file, it is a good choice, because it provides a simpler interface than DOM, and it does not need to read the entire XML file into the memory at a time, this can be used to read large files.

The QtXml module of Qt provides a QXmlSimpleReader class, which is a SAX-based XML parser. This parser is based on events, but these events are associated by themselves and do not need to be set. We only need to know that when the parser parses an XML element, it will execute corresponding events. We only need to rewrite these event processing functions so that they can be parsed according to our ideas.

Summary: AboutQt DatabaseThe content of the simple learning tutorial has been introduced. I 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.