Notes on database programming in Qt

Source: Internet
Author: User

Qt DatabaseProgramming is the content to be introduced in this article.QTMediumDatabaseProgramming andDatabaseFor more information, see this document.

The QtSql module provides a platform-independent and database-independent interface for accessing the SQL database.QtEach database connection in is represented by a QSqlDatabase object. Qt uses different drivers to communicate with APIs of different databases.

QSqlQuery provides the ability to directly execute arbitrary SQL statements. In addition, it provides two high-level database interfaces without SQL commands:

 
 
  1. QSqlTableModel and QSqlRelationalTableModelSection1.ConnectingandQuerying

Before executing SQL commands, you must establish a connection to the same database.

The static function QSqlDatabase: addDatabase () is used to create a new QSqlDatabase object. The first parameter of the function specifies which Driver Qt should select to access the database.

After setting hostname, databasename, username, and password for the created QSqlDatabase object, you need to call the open () function to establish a connection to the database.

Once the link to the database is established, you can use QSqlQuery: exec () to execute any SQL statements supported by the underlying database.

QSqlQuery: next () returns the next row in the query result set, while QSqlQuery: value () returns the value of one item in the current row, in the form of QVariant.

You can use QSqlQuery: isActive () to check whether an error occurs during SQL statement execution.

 
 
  1. placeholder  
  2. QSqlQuery::prepare()  
  3. QSqlQuery::bindValue()orQSqlQuery::addBindValue()  
  4. QSqlQuery::exec() 

QtSupportedDatabaseThe concept of transaction in. Transaction () is used to start transaction, while commit () or rollback () is used to end transaction.

The static function QSqlDatabase: database () returns the QSqlDatabase object corresponding to the specified connection.

 
 
  1. QSqlDatabase: driver () returns the dirver used at the underlying layer of the connection.
  2. QSqlDatabase: hasFeature () can be used to query whether the underlying database supports a certain feature.

Qt allows you to create multiple database connections in a program. In this case, when executing an SQL statement, you need to input the QSqlDatabase object corresponding to the database to execute the statement for the QSqlQuery constructor.

Compared with QSqlQuery, QSqlTableModel provides a higher level and more abstract interface to avoid using original SQL commands.

 
 
  1. QSqlTableModel::record()&QSqlTableModel::value()  
  2. QSqlTableModel::insertRow()&QSqlTableModel::setData()  

QSqlTableModel: submitAll (). Unlike other models, when using QSqlTableModel, submitAll () must be called to force all modifications to be written to the database.

To process foreign keys, use QSqlRelationalTableModel instead of QSqlTableModel.

For applications that use SQL-related classes, you need to add the following line in the corresponding. pro: "QT + = SQL". In this way, the QtSql library will be linked.

Summary: AboutQtMediumDatabaseThe content of the study notes for programming is complete. I hope this article will help you!

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.