TQSqlQuery encapsulates the functionality involved in creating, navigating and retrieving data from SQL queries which are executed on a TQSqlDatabase. It can be used to execute DML (data manipulation language) statements, e.g. \fCSELECT\fR, \fCINSERT\fR, \fCUPDATE\fR and \fCDELETE\fR, and also DDL (data definition language) statements, e.g. \fCCREATE TABLE\fR. It can also be used to execute database-specific commands which are not standard SQL (e.g. \fCSET DATESTYLE=ISO\fR for PostgreSQL).
Successfully executed SQL statements set the query's state to active (isActive() returns TRUE); otherwise the query's state is set to inactive. In either case, when executing a new SQL statement, the query is positioned on an invalid record; an active query must be navigated to a valid record (so that isValid() returns TRUE) before values can be retrieved.
.PP
Navigating records is performed with the following functions:
.TP
next()
.TP
prev()
.TP
first()
.TP
last()
.TP
\fC\fRseek(int)
.PP
These functions allow the programmer to move forward, backward or arbitrarily through the records returned by the query. If you only need to move forward through the results, e.g. using next() or using seek() with a positive offset, you can use setForwardOnly() and save a significant amount of memory overhead. Once an active query is positioned on a valid record, data can be retrieved using value(). All data is transferred from the SQL backend using QVariants.
To access the data returned by a query, use the value() method. Each field in the data returned by a SELECT statement is accessed by passing the field's position in the statement, starting from 0. Information about the fields can be obtained via TQSqlDatabase::record(). For the sake of efficiency there are no functions to access a field by name. (The TQSqlCursor class provides a higher-level interface with field access by name and automatic SQL generation.)
TQSqlQuery supports prepared query execution and the binding of parameter values to placeholders. Some databases don't support these features, so for them TQt emulates the required functionality. For example, the Oracle and ODBC drivers have proper prepared query support, and TQt makes use of it; but for databases that don't have this support, TQt implements the feature itself, e.g. by replacing placeholders with actual values when a query is executed. The exception is positional binding using named placeholders, which requires that the database supports prepared queries.
Oracle databases identify placeholders by using a colon-name syntax, e.g \fC:name\fR. ODBC simply uses \fC?\fR characters. TQt supports both syntaxes (although you can't mix them in the same query).
\fBNote:\fR Using positional binding with named placeholders will only work if the database supports prepared queries. This can be checked with TQSqlDriver::hasFeature() using TQSqlDriver::PreparedQueries as argument for driver feature.
query.prepare( "INSERT INTO atable (id, forename, surname) "
.br
"VALUES (?, ?, ?)" );
.br
query.bindValue( 0, 1001 );
.br
query.bindValue( 1, "Bart" );
.br
query.bindValue( 2, "Simpson" );
.br
query.exec();
.br
.fi
.PP
\fBBinding values using positional placeholders #2\fR
.PP
.nf
.br
query.prepare( "INSERT INTO atable (id, forename, surname) "
.br
"VALUES (?, ?, ?)" );
.br
query.addBindValue( 1001 );
.br
query.addBindValue( "Bart" );
.br
query.addBindValue( "Simpson" );
.br
query.exec();
.br
.fi
.PP
\fBBinding values to a stored procedure\fR This code calls a stored procedure called \fCAsciiToInt()\fR, passing it a character through its in parameter, and taking its result in the out parameter.
Creates a TQSqlQuery object using the SQL \fIquery\fR and the database \fIdb\fR. If \fIdb\fR is 0, (the default), the application's default database is used. If \fIquery\fR is not a null string, it will be executed.
Adds the value \fIval\fR to the list of values when using positional value binding. The order of the addBindValue() calls determines which placeholder a value will be bound to in the prepared query. If \fItype\fR is TQSql::Out or TQSql::InOut, the placeholder will be overwritten with data from the database after the exec() call.
Returns the current internal position of the query. The first record is at position zero. If the position is invalid, a TQSql::Location will be returned indicating the invalid position.
Set the placeholder \fIplaceholder\fR to be bound to value \fIval\fR in the prepared statement. Note that the placeholder mark (e.g \fC:\fR) must be included when specifying the placeholder name. If \fItype\fR is TQSql::Out or TQSql::InOut, the placeholder will be overwritten with data from the database after the exec() call.
Set the placeholder in position \fIpos\fR to be bound to value \fIval\fR in the prepared statement. Field numbering starts at 0. If \fItype\fR is TQSql::Out or TQSql::InOut, the placeholder will be overwritten with data from the database after the exec() call.
Executes the SQL in \fIquery\fR. Returns TRUE and sets the query state to active if the query was successful; otherwise returns FALSE and sets the query state to inactive. The \fIquery\fR string must use syntax appropriate for the SQL database being queried, for example, standard SQL.
.PP
After the query is executed, the query is positioned on an \fIinvalid\fR record, and must be navigated to a valid record before data values can be retrieved, e.g. using next().
.PP
Note that the last error for this query is reset when exec() is called.
.PP
See also isActive(), isValid(), next(), prev(), first(), last(), and seek().
.PP
Examples:
.)l sql/overview/basicbrowsing/main.cpp, sql/overview/basicbrowsing2/main.cpp, sql/overview/basicdatamanip/main.cpp, and sql/overview/connection.cpp.
In most cases this function returns the same as lastQuery(). If a prepared query with placeholders is executed on a DBMS that does not support it, the preparation of this query is emulated. The placeholders in the original query are replaced with their bound values to form a new query. This function returns the modified query. Useful for debugging purposes.
Retrieves the first record in the result, if available, and positions the query on the retrieved record. Note that the result must be in an active state and isSelect() must return TRUE before calling this function or it will do nothing and return FALSE. Returns TRUE if successful. If unsuccessful the query position is set to an invalid position and FALSE is returned.
.PP
See also next(), prev(), last(), seek(), at(), isActive(), and isValid().
Returns TRUE if the query is active and positioned on a valid record and the \fIfield\fR is NULL; otherwise returns FALSE. Note that for some drivers isNull() will not return accurate information until after an attempt is made to retrieve data.
Retrieves the last record in the result, if available, and positions the query on the retrieved record. Note that the result must be in an active state and isSelect() must return TRUE before calling this function or it will do nothing and return FALSE. Returns TRUE if successful. If unsuccessful the query position is set to an invalid position and FALSE is returned.
.PP
See also next(), prev(), first(), seek(), at(), isActive(), and isValid().
Retrieves the next record in the result, if available, and positions the query on the retrieved record. Note that the result must be in an active state and isSelect() must return TRUE before calling this function or it will do nothing and return FALSE.
.PP
The following rules apply:
.TP
If the result is currently located before the first record, e.g. immediately after a query is executed, an attempt is made to retrieve the first record.
.IP
.TP
If the result is currently located after the last record, there is no change and FALSE is returned.
.IP
.TP
If the result is located somewhere in the middle, an attempt is made to retrieve the next record.
.PP
If the record could not be retrieved, the result is positioned after the last record and FALSE is returned. If the record is successfully retrieved, TRUE is returned.
.PP
See also prev(), first(), last(), seek(), at(), isActive(), and isValid().
.PP
Examples:
.)l sql/overview/basicbrowsing/main.cpp, sql/overview/basicbrowsing2/main.cpp, sql/overview/delete/main.cpp, sql/overview/order1/main.cpp, sql/overview/retrieve1/main.cpp, sql/overview/subclass4/main.cpp, and sql/overview/subclass5/main.cpp.
Returns the number of rows affected by the result's SQL statement, or -1 if it cannot be determined. Note that for \fCSELECT\fR statements, the value is undefined; see size() instead. If the query is not active (isActive() returns FALSE), -1 is returned.
Prepares the SQL query \fIquery\fR for execution. The query may contain placeholders for binding values. Both Oracle style colon-name (e.g. \fC:surname\fR), and ODBC style (e.g. \fC?\fR) placeholders are supported; but they cannot be mixed in the same query. See the Description for examples.
Retrieves the previous record in the result, if available, and positions the query on the retrieved record. Note that the result must be in an active state and isSelect() must return TRUE before calling this function or it will do nothing and return FALSE.
.PP
The following rules apply:
.TP
If the result is currently located before the first record, there is no change and FALSE is returned.
.IP
.TP
If the result is currently located after the last record, an attempt is made to retrieve the last record.
.IP
.TP
If the result is somewhere in the middle, an attempt is made to retrieve the previous record.
.PP
If the record could not be retrieved, the result is positioned before the first record and FALSE is returned. If the record is successfully retrieved, TRUE is returned.
.PP
See also next(), first(), last(), seek(), at(), isActive(), and isValid().
Retrieves the record at position (offset) \fIi\fR, if available, and positions the query on the retrieved record. The first record is at position 0. Note that the query must be in an active state and isSelect() must return TRUE before calling this function.
.PP
If \fIrelative\fR is FALSE (the default), the following rules apply:
.TP
If \fIi\fR is negative, the result is positioned before the first record and FALSE is returned.
.TP
Otherwise, an attempt is made to move to the record at position \fIi\fR. If the record at position \fIi\fR could not be retrieved, the result is positioned after the last record and FALSE is returned. If the record is successfully retrieved, TRUE is returned.
.PP
If \fIrelative\fR is TRUE, the following rules apply:
.TP
If the result is currently positioned before the first record or on the first record, and \fIi\fR is negative, there is no change, and FALSE is returned.
.TP
If the result is currently located after the last record, and \fIi\fR is positive, there is no change, and FALSE is returned.
.TP
If the result is currently located somewhere in the middle, and the relative offset \fIi\fR moves the result below zero, the result is positioned before the first record and FALSE is returned.
.TP
Otherwise, an attempt is made to move to the record \fIi\fR records ahead of the current record (or \fIi\fR records behind the current record if \fIi\fR is negative). If the record at offset \fIi\fR could not be retrieved, the result is positioned after the last record if \fIi\fR >= 0, (or before the first record if \fIi\fR is negative), and FALSE is returned. If the record is successfully retrieved, TRUE is returned.
.PP
See also next(), prev(), first(), last(), at(), isActive(), and isValid().
Sets forward only mode to \fIforward\fR. If forward is TRUE only next(), and seek() with positive values, are allowed for navigating the results. Forward only mode needs far less memory since results do not need to be cached.
Returns the size of the result, (number of rows returned), or -1 if the size cannot be determined or if the database does not support reporting information about query sizes. Note that for non-\fCSELECT\fR statements (isSelect() returns FALSE), size() will return -1. If the query is not active (isActive() returns FALSE), -1 is returned.
.PP
To determine the number of rows affected by a non-SELECT statement, use numRowsAffected().
Returns the value of the \fIi\fR-th field in the query (zero based).
.PP
The fields are numbered from left to right using the text of the \fCSELECT\fR statement, e.g. in \fCSELECT forename, surname FROM people\fR, field 0 is \fCforename\fR and field 1 is \fCsurname\fR. Using \fCSELECT *\fR is not recommended because the order of the fields in the query is undefined.
.PP
An invalid QVariant is returned if field \fIi\fR does not exist, if the query is inactive, or if the query is positioned on an invalid record.
.PP
See also prev(), next(), first(), last(), seek(), isActive(), and isValid().
.PP
Examples:
.)l sql/overview/basicbrowsing/main.cpp, sql/overview/basicbrowsing2/main.cpp, sql/overview/retrieve1/main.cpp, sql/overview/subclass3/main.cpp, sql/overview/subclass4/main.cpp, sql/overview/subclass5/main.cpp, and sql/overview/table4/main.cpp.