QT Database Programming _ Basic

Source: Internet
Author: User

The Qtsql module provides a platform-independent, database-independent interface for accessing SQL databases.

Each database connection in QT is represented by a Qsqldatabase object, and Qt uses different driver to communicate with various APIs of different databases.

Qsqlquery provides the characteristics of executing arbitrary SQL statements directly, and also provides two high-level database interfaces that do not require SQL commands: Qsqltablemodel and Qsqlrelationaltablemodel
Section 1. Connecting and querying

Before executing the SQL command, you must first establish a connection to the database.

The static function Qsqldatabase::adddatabase () is used to create a new Qsqldatabase object, and the first parameter of the function specifies which driver the QT chooses to access the database.

After setting the host Name,database name, 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 has been established, it is possible to execute any SQL statements supported by the underlying database through Qsqlquery::exec ().

Qsqlquery::next () returns the next row in the query result set, and Qsqlquery::value () returns the value of an item in the current row, returned as a qvariant.

You can use Qsqlquery::isactive () to check the execution of an SQL statement for errors.

Placeholder
Qsqlquery::p repare ()
Qsqlquery::bindvalue () or Qsqlquery::addbindvalue ()
Qsqlquery::exec ()

The concept of transaction (transaction) in the QT support database. Transaction () is used to start transaction, while commit () or rollback () is used to end transaction.

static function Qsqldatabase::d atabase () that returns the Qsqldatabase object for the specified connection.

Qsqldatabase::d River () returns the dirver used at the bottom of the connection
Qsqldatabase::hasfeature () can be used to query whether an attribute is supported by the underlying database.

QT allows multiple database connections to be created in a single program, in which case the SQL statement needs to pass in the Qsqlquery constructor for the Qsqldatabase object that corresponds to the database in which the statement is executed.

Compared to Qsqlquery, Qsqltablemodel provides a higher level, more abstract interface, which avoids the use of raw SQL commands.

Qsqltablemodel::record () & Qsqltablemodel::value ()

Qsqltablemodel::insertrow () & Qsqltablemodel::setdata ()

Qsqltablemodel::submitall (), unlike other model, when using Qsqltablemodel, you must call Submitall () to force all modifications to be written to the database.

When you need to process foreign keys (foreign key), you need to use Qsqlrelationaltablemodel instead of Qsqltablemodel.

For applications that use SQL-related classes, you need to add the following line to the corresponding. Pro: "QT +=sql", which links the Qtsql library.

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.