CopperSpice API
1.9.2
|
The QSqlQuery class provides an interface for executing SQL statements and navigating through the result set of a query. The QSqlQueryModel and QSqlTableModel classes described in the next section provide a higher level interface for accessing databases.
To execute an SQL statement create a QSqlQuery object and call QSqlQuery::exec().
The QSqlQuery constructor accepts an optional QSqlDatabase object that specifies which database connection to use. In the example above, we do not specify any connection, so the default connection is used. If an error occurs, QSqlQuery::exec() returns false. The error is then available as QSqlQuery::lastError().
QSqlQuery provides access to a result set one record at a time. After the call to QSqlQuery::exec() the cursor is located one position before the first record in the result set. You need to call QSqlQuery::next() once to advance to the first record and then call it repeatedly to access the subsequent records until it returns false.
The cursor in the QSqlQuery object can be moved by calling QSqlQuery::next(), QSqlQuery::previous(), QSqlQuery::first(), QSqlQuery::last(), and QSqlQuery::seek(). If you iterate through a result set only using next() and seek() with positive values, you can call QSqlQuery::setForwardOnly(true) before calling exec(). This is an easy optimization that will speed up the query significantly when operating on large result sets.
The QSqlQuery::value() method returns the value of a field in the current record. Fields are specified using zero based indexes. QSqlQuery::value() returns a QVariant which can hold various data types such as int, QString, and QByteArray. The data in the database is automatically mapped to the closest CopperSpice equivalent type.
In the prior example, QVariant::toString() and QVariant::toInt() are used to convert variant values to QString and int values. For an overview of the recommended types for use with CopperSpice supported Databases, refer to Data Types.
To determine whether a database driver supports a given feature use QSqlDriver::hasFeature(). In the following example we call QSqlQuery::size() to determine the size of a result set if the underlying database supports this feature, otherwise, we navigate to the last record and use the query's position to tell us how many records there are.
QSqlQuery can execute any SQL statement. The following example inserts a record into a table using an INSERT statement.
If you want to insert multiple records at the same time it is often more efficient to separate the query from the actual values being inserted. This can be done using placeholders. CopperSpice supports two placeholder syntaxes, named binding and positional binding.
Both forms work with all database drivers provided by CopperSpice. If the database supports the syntax natively CopperSpice simply forwards the query to the DBMS, otherwise CopperSpice simulates the placeholder syntax by preprocessing the query. The actual query which ends up being executed is available as QSqlQuery::executedQuery().
When using prepared statements you only need to call QSqlQuery::prepare() once. Then call QSqlQuery::bindValue() or QSqlQuery::addBindValue() followed by QSqlQuery::exec() as many times as necessary. Besides performance, one advantage of placeholders is that you can easily specify arbitrary values without having to worry about escaping special characters.
Updating or deleting a record is similar to inserting it into a table.
Transactions can be used to ensure that a complex operation is atomic (for example, looking up a foreign key and creating a record), or to provide a means of canceling a complex change in the middle.
If the underlying database engine supports transactions, QSqlDriver::hasFeature(QSqlDriver::Transactions) will return true. You can use QSqlDatabase::transaction() to initiate a transaction, followed by the SQL commands you want to execute within the context of the transaction, and then either QSqlDatabase::commit() or QSqlDatabase::rollback(). When using transactions you must start the transaction before you create your query.