About Qt Database Programming

Source: Internet
Author: User

Qt DatabaseProgramming is the content to be introduced in this article, mainly to learnQTMediumDatabase. Now let's talk about how to useDatabaseSELECT INSERT UPDATE DELETEDatabaseInDatabaseThe QSqlTableModel must be used when data is displayed under a graph. ThereforeDatabaseBefore programming, you must master the basic SQL command statements.

Database driver layer

The associated classes include QSqlDriver, QSqlDriverCreatpor, QSqlDriverCreatorBase, QSqlDriverPlugin, and QSqlResult.

Database Application Interface Layer

These classes provide access to the database and initiate a connection request. Just like a file access request, when the connection is successful, information is returned by judging the information, you can use the QSqlQuery class to operate the database once a connection is established. After connecting to the database, several classes are provided, such as QSqlError, QSqlField, QSqlIndex, and QSqlRecord.

User Interface Layer

These classes provide data-related components, including QSqlQueryModel, QSqlTableModel, and QSqlRelationTableModel. The definitions of these classes are used to provide database patterns and try to design them.

Connect to database

If you want to establish a connection to a database, first you need to know which database to use and load the driver for the connection to the database. If it is a mysql database, there will be a user name and password, this must also be set. The connected database may be local or on a remote computer, so you need to set a host name to distinguish it.

The following is an example of connecting to a database:

 
 
  1. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");  
  2. db.setHostname("bigblue");  
  3. db.setDatabaseName("flightdb");  
  4. db.setUserName("acarlson");  
  5. db.setPassword("1uTbSbAs");  
  6. bool k = db.open(); 

We can also establish a connection to two databases:

 
 
  1. QSqlDatabase firstDB = QSqlDatabase::addDatabase("QMYSQL", "first");  
  2. QSqlDatabase secondDB = QSqlDatabase::addDatabase("QMYSQL", "second"); 

An error may occur when you open the database. Here, a static function, QSqlDatabase: lastError (), is provided to return the current error. When opening a data file, you need to close the database after the operation, use QSqlDatabase: close (), and then call QSqlDatabase: removeDatabase ().

Execute database statements

QSqlQuery provides a method for executing database statements. It returns all execution results. After a database connection is established, you can use QSqlQuery: exec () as follows:

 
 
  1. QSqlQuery query;  
  2. query.exec("SELECT name, salary FROM employee WHERE salary > 50000"); 

After a constructor is created for QSqlQuery, it will accept the connection of a specific QSqlDatabase object, just as in the above Code.

View query results

After executing exec (), QSqlQuery places the pointer on the first record of the record. Therefore, you need to call QSqlQuery: next () to obtain the code below the first data, use a circular body to facilitate data in all tables:

 
 
  1. while (query.next()) {  
  2. QString name = query.value(0).toString();  
  3. int salary = query.value(1).toInt();  
  4. qDebug() << name << salary;  

The QSqlQuery: value () function returns a QVariant type as the default QSqlValue: value. Supports several optional types. They are basic types of C ++, such as int QString and QByteArray. For different types of conversions, use the functions provided by Qt, such as QVariant: toString and QVariant: toInt ().

Summary: AboutQt DatabaseThe content of programming 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.