Working with databases in Qt
Qt makes it possible to create platform-independent database applications using standard DBMSs. Qt includes native drivers for Oracle, Microsoft SQL Server, Sybase Adaptive Server, IBM DB2, PostgreSQL, MySQL, and ODBC-compatible databases. Qt includes database-specific widgets, and also supports the extension for working with databases of any built-in or separately written widgets.
Working with databases in Qt takes place at various levels:
1. Driver Layer - Includes the QSqlDriver, QSqlDriverCreator, QSqlDriverCreatorBase, QSqlDriverPlugin and QSqlResult classes. This layer provides a low-level bridge between specific databases and the SQL API layer.
2.Layer SQL API - This layer provides access to databases. Connections are established using the QSqlDatabase class. Interaction with the database is carried out using the QSqlQuery class. In addition to the QSqlDatabase and QSqlQuery classes, the SQL API layer relies on the QSqlError, QSqlField, QSqlIndex, and QsqlRecord classes.
3. User Interface Layer - This layer associates data from a database with data-oriented widgets. This includes classes such as QSqlQueryModel, QSqlTableModel, and QSqlRelationalTableModel.
To access the database using QSqlQuery and QSqlQueryModel, you must create and open one or more database connections.
Qt can work with the following databases (due to incompatibility with the GPL license, not all plugins come with Qt Open Source Edition):
To build the driver plug-in, which are not included in the Qt package, you need to have the appropriate client library for the DBMS used.
You can connect to the database like this:
The first line creates the connection object, and the last opens it. In the interval, some connection information is initialized, including connection name, database name, host name, username, password. This example connects to the flightdb MySQL database on the bigblue node. The argument “QMYSQL” in addDatabase () indicates the type of database driver to use for the connection, and “mydb” is the name of the connection.
Once the connection is established, you can call the static function QSqlDatabase :: database () from anywhere in the program with the name of the connection to get a pointer to this connection. If you do not pass the connection name, it will return the connection by default.
If open () fails, it will return false. In this case, you can get information about the error by calling QSqlDatabase :: lastError ().
To remove a connection to the database, you must first close the database using QSqlDatabase :: close (), and then remove the connection using the static method QSqlDatabase :: removeDatabase ().
The QSqlQuery class provides an interface for executing SQL queries and navigating through the result set.
To execute SQL queries, simply create a QSqlQuery object and call QSqlQuery :: exec (). For example, like this:
The QSqlQuery constructor takes an optional QSqlDatabase argument, which specifies which database connection is used. If not specified, then the default connection is used.
If an error occurs, exec () returns false. The error can be accessed using QSqlQuery :: lastError ().
QSqlQuery provides one-time access to the result set of a single query. After exec () is called, the internal QSqlQuery pointer points to the position before the first record. If you call the QSqlQuery :: next () method once, it will move the pointer to the first record. After that, you need to repeat the next () call to access other entries until it returns false. Here is a typical loop that goes through all the records in order:
QSqlQuery can perform not only SELECT, but also any other queries. The following example inserts a record into a table using INSERT:
If you need to insert multiple records at the same time, it is often more efficient to separate the query from the actual values inserted. This can be done by inserting values through parameters. Qt supports two value insertion syntaxes: named parameters and positional parameters. The following example shows insertion using a named parameter:
This example shows insertion using a positional parameter:
When inserting multiple records, you only need to call QSqlQuery :: prepare () once. You can then call bindValue () or addBindValue (), followed by calling exec () as many times as needed.
The QSqlQueryModel, QSqlTableModel, and QSqlRelationalTableModel classes can be used as data sources for Qt view classes such as QListView, QTableView, and QTreeView. In practice, QTableView is most often used due to the fact that the resulting SQL selection is essentially a two-dimensional data structure.
The following example creates a view based on an SQL data model:
If the model is a read-write model (for example, QSqlTableModel), then the view allows you to edit the fields. This can be disabled using the following code
You can use the same model as a data source for multiple views. If the user changes the model data using one of the views, the other views will immediately display the changes.
Presentation classes to indicate columns at the top display headings. To change the title text, use the setHeaderData () function of the model. For instance:
This article outlines the basic principles of working with databases in Qt. However, in addition to the possibilities indicated here, there are still many interesting things, for example, transactions, working with foreign keys or creating data-oriented forms. Unfortunately, these topics are extensive enough for one article.
UPD: fixed errors in the code.
Introduction
Working with databases in Qt takes place at various levels:
1. Driver Layer - Includes the QSqlDriver, QSqlDriverCreator, QSqlDriverCreatorBase, QSqlDriverPlugin and QSqlResult classes. This layer provides a low-level bridge between specific databases and the SQL API layer.
2.Layer SQL API - This layer provides access to databases. Connections are established using the QSqlDatabase class. Interaction with the database is carried out using the QSqlQuery class. In addition to the QSqlDatabase and QSqlQuery classes, the SQL API layer relies on the QSqlError, QSqlField, QSqlIndex, and QsqlRecord classes.
3. User Interface Layer - This layer associates data from a database with data-oriented widgets. This includes classes such as QSqlQueryModel, QSqlTableModel, and QSqlRelationalTableModel.
Database connection
To access the database using QSqlQuery and QSqlQueryModel, you must create and open one or more database connections.
Qt can work with the following databases (due to incompatibility with the GPL license, not all plugins come with Qt Open Source Edition):
- QDB2 - IBM DB2 (version 7.1 and higher
- QIBASE - Borland InterBase
- QMYSQL - MySQL
- QOCI - Oracle Call Interface Driver
- QODBC - Open Database Connectivity (ODBC) - Microsoft SQL Server and other ODBC-compatible databases
- QPSQL - PostgreSQL (version 7.3 and higher)
- QSQLITE2 - SQLite version 2
- QSQLITE - SQLite version 3
- QTDS - Sybase Adaptive Server Driver
To build the driver plug-in, which are not included in the Qt package, you need to have the appropriate client library for the DBMS used.
You can connect to the database like this:
- QSqlDatabase db = QsqlDatabase::addDatabase("QMYSQL", "mydb");
- db.setHostName("bigblue");
- db.setDatabaseName("flightdb");
- db.setUserName("acarlson");
- db.setPassword("1uTbSbAs");
- bool ok = db.open();
* This source code was highlighted with Source Code Highlighter.
The first line creates the connection object, and the last opens it. In the interval, some connection information is initialized, including connection name, database name, host name, username, password. This example connects to the flightdb MySQL database on the bigblue node. The argument “QMYSQL” in addDatabase () indicates the type of database driver to use for the connection, and “mydb” is the name of the connection.
Once the connection is established, you can call the static function QSqlDatabase :: database () from anywhere in the program with the name of the connection to get a pointer to this connection. If you do not pass the connection name, it will return the connection by default.
If open () fails, it will return false. In this case, you can get information about the error by calling QSqlDatabase :: lastError ().
To remove a connection to the database, you must first close the database using QSqlDatabase :: close (), and then remove the connection using the static method QSqlDatabase :: removeDatabase ().
Executing SQL Statements
The QSqlQuery class provides an interface for executing SQL queries and navigating through the result set.
To execute SQL queries, simply create a QSqlQuery object and call QSqlQuery :: exec (). For example, like this:
- QSqlQuery query;
- query.exec("SELECT name, salary FROM employee WHERE salary > 50000");
* This source code was highlighted with Source Code Highlighter.
The QSqlQuery constructor takes an optional QSqlDatabase argument, which specifies which database connection is used. If not specified, then the default connection is used.
If an error occurs, exec () returns false. The error can be accessed using QSqlQuery :: lastError ().
QSqlQuery provides one-time access to the result set of a single query. After exec () is called, the internal QSqlQuery pointer points to the position before the first record. If you call the QSqlQuery :: next () method once, it will move the pointer to the first record. After that, you need to repeat the next () call to access other entries until it returns false. Here is a typical loop that goes through all the records in order:
- while (query.next()) {
- QString name = query.value(0).toString();
- int salary = query.value(1).toInt();
- qDebug() << name << salary;
- }
* This source code was highlighted with Source Code Highlighter.
QSqlQuery can perform not only SELECT, but also any other queries. The following example inserts a record into a table using INSERT:
- QSqlQuery query;
- query.exec("INSERT INTO employee (id, name, salary) "
- "VALUES (1001, 'Thad Beaumont', 65000)");
* This source code was highlighted with Source Code Highlighter.
If you need to insert multiple records at the same time, it is often more efficient to separate the query from the actual values inserted. This can be done by inserting values through parameters. Qt supports two value insertion syntaxes: named parameters and positional parameters. The following example shows insertion using a named parameter:
- QSqlQuery query;
- query.prepare("INSERT INTO employee (id, name, salary) "
- "VALUES (:id, :name, :salary)");
- query.bindValue(":id", 1001);
- query.bindValue(":name", "Thad Beaumont");
- query.bindValue(":salary", 65000);
- query.exec();
* This source code was highlighted with Source Code Highlighter.
This example shows insertion using a positional parameter:
- QSqlQuery query;
- query.prepare("INSERT INTO employee (id, name, salary) "
- "VALUES (?, ?, ?)");
- query.addBindValue(1001);
- query.addBindValue("Thad Beaumont");
- query.addBindValue(65000);
- query.exec();
* This source code was highlighted with Source Code Highlighter.
When inserting multiple records, you only need to call QSqlQuery :: prepare () once. You can then call bindValue () or addBindValue (), followed by calling exec () as many times as needed.
Displaying data in a table view
The QSqlQueryModel, QSqlTableModel, and QSqlRelationalTableModel classes can be used as data sources for Qt view classes such as QListView, QTableView, and QTreeView. In practice, QTableView is most often used due to the fact that the resulting SQL selection is essentially a two-dimensional data structure.
The following example creates a view based on an SQL data model:
- QSqlTableModel model;
- model.setTable("employee");
- QTableView *view = new QTableView;
- view->setModel(&model);
- view->show();
* This source code was highlighted with Source Code Highlighter.
If the model is a read-write model (for example, QSqlTableModel), then the view allows you to edit the fields. This can be disabled using the following code
- view->setEditTriggers(QAbstractItemView::NoEditTriggers);
* This source code was highlighted with Source Code Highlighter.
You can use the same model as a data source for multiple views. If the user changes the model data using one of the views, the other views will immediately display the changes.
Presentation classes to indicate columns at the top display headings. To change the title text, use the setHeaderData () function of the model. For instance:
- model->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
- model->setHeaderData(1, Qt::Horizontal, QObject::tr("Name"));
- model->setHeaderData(2, Qt::Horizontal, QObject::tr("City"));
- model->setHeaderData(3, Qt::Horizontal, QObject::tr("Country"));
* This source code was highlighted with Source Code Highlighter.
Conclusion
This article outlines the basic principles of working with databases in Qt. However, in addition to the possibilities indicated here, there are still many interesting things, for example, transactions, working with foreign keys or creating data-oriented forms. Unfortunately, these topics are extensive enough for one article.
UPD: fixed errors in the code.