|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.trolltech.qt.internal.QSignalEmitterInternal
com.trolltech.qt.QSignalEmitter
com.trolltech.qt.QtJambiObject
com.trolltech.qt.sql.QSqlQuery
public class QSqlQuery
The QSqlQuery
class provides a means of executing and manipulating SQL statements. QSqlQuery
encapsulates the functionality involved in creating, navigating and retrieving data from SQL queries which are executed on a QSqlDatabase
. It can be used to execute DML (data manipulation language) statements, such as SELECT, INSERT, UPDATE and DELETE, as well as DDL (data definition language) statements, such as CREATETABLE. It can also be used to execute database-specific commands which are not standard SQL (e.g. SET DATESTYLE=ISO for PostgreSQL).
Successfully executed SQL statements set the query's state to active; isActive()
then 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. Navigating records is performed with the following functions: 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., by using next()
), you can use setForwardOnly()
, which will save a significant amount of memory overhead and improve performance on some databases. 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.
For example:
QSqlQuery query = new QSqlQuery("SELECT * country FROM artist"); while (query.next()) { String country = query.value(0).toString(); doSomething(country); }To access the data returned by a query, use value(int). Each field in the data returned by a SELECT statement is accessed by passing the field's position in the statement, starting from 0. This makes using SELECT * queries inadvisable because the order of the fields returned is indeterminate.
For the sake of efficiency, there are no functions to access a field by name (unless you use prepared queries with names, as explained below). To convert a field name into an index, use record()
.indexOf()
, for example:
QSqlQuery query = new QSqlQuery("SELECT * FROM artist"); int fieldNo = query.record().indexOf("country"); while (query.next()) { String country = query.value(fieldNo).toString(); doSomething(country); }
QSqlQuery
supports prepared query execution and the binding of parameter values to placeholders. Some databases don't support these features, so for those, Qt emulates the required functionality. For example, the Oracle and ODBC drivers have proper prepared query support, and Qt makes use of it; but for databases that don't have this support, Qt implements the feature itself, e.g. by replacing placeholders with actual values when a query is executed. Use numRowsAffected()
to find out how many rows were affected by a non-SELECT query, and size()
to find how many were retrieved by a SELECT. Oracle databases identify placeholders by using a colon-name syntax, e.g :name. ODBC simply uses ? characters. Qt supports both syntaxes, with the restriction that you can't mix them in the same query.
You can retrieve the values of all the fields in a single variable (a map) using Named binding using named placeholders: This code calls a stored procedure called AsciiToInt(), passing it a character through its in parameter, and taking its result in the out parameter. Stored procedures that uses the return statement to return values, or return multiple result sets, are not fully supported. For specific details see SQL Database Drivers. Warning: You must load the SQL driver and open the connection before a
boundValues()
.Approaches to Binding Values
Below we present the same example using each of the four different binding approaches, as well as one example of binding values to a stored procedure.
QSqlQuery query = new QSqlQuery();
query.prepare("INSERT INTO person (id, forename, surname) " +
"VALUES (:id, :forename, :surname)");
query.bindValue(":id", 1001);
query.bindValue(":forename", "Bart");
query.bindValue(":surname", "Simpson");
query.exec();
Positional binding using named placeholders:
QSqlQuery query = new QSqlQuery();
query.prepare("INSERT INTO person (id, forename, surname) " +
"VALUES (:id, :forename, :surname)");
query.bindValue(0, 1001);
query.bindValue(1, "Bart");
query.bindValue(2, "Simpson");
query.exec();
Binding values using positional placeholders (version 1):
QSqlQuery query = new QSqlQuery();
query.prepare("INSERT INTO person (id, forename, surname) " +
"VALUES (?, ?, ?)");
query.bindValue(0, 1001);
query.bindValue(1, "Bart");
query.bindValue(2, "Simpson");
query.exec();
Binding values using positional placeholders (version 2):
QSqlQuery query = new QSqlQuery();
query.prepare("INSERT INTO person (id, forename, surname) " +
"VALUES (?, ?, ?)");
query.addBindValue(1001);
query.addBindValue("Bart");
query.addBindValue("Simpson");
query.exec();
Binding values to a stored procedure:
QSqlQuery query = new QSqlQuery();
query.prepare("CALL AsciiToInt(?, ?)");
query.bindValue(0, "A");
query.bindValue(1, 0, QSql.ParamTypeFlag.Out);
query.exec();
int i = query.boundValue(1).toInt(); // i is 65
Note that unbound parameters will retain their values. QSqlQuery
is created. Also, the connection must remain open while the query exists; otherwise, the behavior of QSqlQuery
is undefined. QSqlDatabase
, QSqlQueryModel
, QSqlTableModel
, and QVariant
.
Nested Class Summary | |
---|---|
static class |
QSqlQuery.BatchExecutionMode
|
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter |
---|
QSignalEmitter.AbstractSignal, QSignalEmitter.Signal0, QSignalEmitter.Signal1, QSignalEmitter.Signal2, QSignalEmitter.Signal3, QSignalEmitter.Signal4, QSignalEmitter.Signal5, QSignalEmitter.Signal6, QSignalEmitter.Signal7, QSignalEmitter.Signal8, QSignalEmitter.Signal9 |
Nested classes/interfaces inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
com.trolltech.qt.internal.QSignalEmitterInternal.AbstractSignalInternal |
Field Summary |
---|
Fields inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
currentSender |
Constructor Summary | |
---|---|
QSqlQuery()
Constructs a QSqlQuery object using the SQL query and the database db. |
|
QSqlQuery(QSqlDatabase db)
Constructs a QSqlQuery object using the database db. |
|
QSqlQuery(QSqlQuery other)
Constructs a copy of other. |
|
QSqlQuery(QSqlResult r)
Constructs a QSqlQuery object which uses the QSqlResult result to communicate with a database. |
|
QSqlQuery(java.lang.String query)
Constructs a QSqlQuery object using the SQL query and the database db. |
|
QSqlQuery(java.lang.String query,
QSqlDatabase db)
Constructs a QSqlQuery object using the SQL query and the database db. |
Method Summary | |
---|---|
void |
addBindValue(java.lang.Object val)
Adds the value val to the list of values when using positional value binding. |
void |
addBindValue(java.lang.Object val,
QSql.ParamType type)
Adds the value val to the list of values when using positional value binding. |
void |
addBindValue(java.lang.Object val,
QSql.ParamTypeFlag[] type)
Adds the value val to the list of values when using positional value binding. |
int |
at()
Returns the current internal position of the query. |
void |
bindValue(int pos,
java.lang.Object val)
Set the placeholder in position pos to be bound to value val in the prepared statement. |
void |
bindValue(int pos,
java.lang.Object val,
QSql.ParamType type)
Set the placeholder in position pos to be bound to value val in the prepared statement. |
void |
bindValue(int pos,
java.lang.Object val,
QSql.ParamTypeFlag[] type)
Set the placeholder in position pos to be bound to value val in the prepared statement. |
void |
bindValue(java.lang.String placeholder,
java.lang.Object val)
Set the placeholder placeholder to be bound to value val in the prepared statement. |
void |
bindValue(java.lang.String placeholder,
java.lang.Object val,
QSql.ParamType type)
Set the placeholder placeholder to be bound to value val in the prepared statement. |
void |
bindValue(java.lang.String placeholder,
java.lang.Object val,
QSql.ParamTypeFlag[] type)
Set the placeholder placeholder to be bound to value val in the prepared statement. |
java.lang.Object |
boundValue(int pos)
Returns the value for the placeholder at position pos. |
java.lang.Object |
boundValue(java.lang.String placeholder)
Returns the value for the placeholder. |
java.util.SortedMap |
boundValues()
Returns a map of the bound values. |
void |
clear()
Clears the result set and releases any resources held by the query. |
QSqlQuery |
clone()
This method is reimplemented for internal reasons |
QSqlDriver |
driver()
Returns the database driver associated with the query. |
boolean |
exec()
Executes a previously prepared SQL query. |
boolean |
exec(java.lang.String query)
Executes the SQL in query. |
boolean |
execBatch()
Executes a previously prepared SQL query in a batch. |
boolean |
execBatch(QSqlQuery.BatchExecutionMode mode)
Executes a previously prepared SQL query in a batch. |
java.lang.String |
executedQuery()
Returns the last query that was successfully executed. |
void |
finish()
Instruct the database driver that no more data will be fetched from this query until it is re-executed. |
boolean |
first()
Retrieves the first record in the result, if available, and positions the query on the retrieved record. |
boolean |
isActive()
Returns true if the query is currently active; otherwise returns false. |
boolean |
isForwardOnly()
Returns true if you can only scroll forward through a result set; otherwise returns false. |
boolean |
isNull(int field)
Returns true if the query is active and positioned on a valid record and the field is NULL; otherwise returns false. |
boolean |
isSelect()
Returns true if the current query is a SELECT statement; otherwise returns false. |
boolean |
isValid()
Returns true if the query is currently positioned on a valid record; otherwise returns false. |
boolean |
last()
Retrieves the last record in the result, if available, and positions the query on the retrieved record. |
QSqlError |
lastError()
Returns error information about the last error (if any) that occurred with this query. |
java.lang.Object |
lastInsertId()
Returns the object ID of the most recent inserted row if the database supports it. |
java.lang.String |
lastQuery()
Returns the text of the current query being used, or an empty string if there is no current query text. |
boolean |
next()
Retrieves the next record in the result, if available, and positions the query on the retrieved record. |
boolean |
nextResult()
Discards the current result set and navigates to the next if available. |
QSql.NumericalPrecisionPolicy |
numericalPrecisionPolicy()
Returns the current precision policy. |
int |
numRowsAffected()
Returns the number of rows affected by the result's SQL statement, or -1 if it cannot be determined. |
boolean |
prepare(java.lang.String query)
Prepares the SQL query query for execution. |
boolean |
previous()
Retrieves the previous record in the result, if available, and positions the query on the retrieved record. |
QSqlRecord |
record()
Returns a QSqlRecord containing the field information for the current query. |
QSqlResult |
result()
Returns the result associated with the query. |
boolean |
seek(int i)
Retrieves the record at position index, if available, and positions the query on the retrieved record. |
boolean |
seek(int i,
boolean relative)
Retrieves the record at position index, if available, and positions the query on the retrieved record. |
void |
setForwardOnly(boolean forward)
Sets forward only mode to forward. |
void |
setNumericalPrecisionPolicy(QSql.NumericalPrecisionPolicy precisionPolicy)
Instruct the database driver to return numerical values with a precision specified by precisionPolicy. |
int |
size()
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. |
java.lang.Object |
value(int i)
Returns the value of field index in the current record. |
Methods inherited from class com.trolltech.qt.QtJambiObject |
---|
dispose, disposed, equals, finalize, reassignNativeResources, tr, tr, tr |
Methods inherited from class com.trolltech.qt.QSignalEmitter |
---|
blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread |
Methods inherited from class com.trolltech.qt.internal.QSignalEmitterInternal |
---|
__qt_signalInitialization |
Methods inherited from class java.lang.Object |
---|
getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Methods inherited from interface com.trolltech.qt.QtJambiInterface |
---|
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership |
Constructor Detail |
---|
public QSqlQuery(QSqlDatabase db)
QSqlQuery
object using the database db. QSqlDatabase
.
public QSqlQuery(QSqlResult r)
QSqlQuery
object which uses the QSqlResult
result to communicate with a database.
public QSqlQuery(QSqlQuery other)
public QSqlQuery(java.lang.String query)
QSqlQuery
object using the SQL query and the database db. If db is not specified, the application's default database is used. If query is not an empty string, it will be executed. QSqlDatabase
.
public QSqlQuery()
QSqlQuery
object using the SQL query and the database db. If db is not specified, the application's default database is used. If query is not an empty string, it will be executed. QSqlDatabase
.
public QSqlQuery(java.lang.String query, QSqlDatabase db)
QSqlQuery
object using the SQL query and the database db. If db is not specified, the application's default database is used. If query is not an empty string, it will be executed. QSqlDatabase
.
Method Detail |
---|
public final void addBindValue(java.lang.Object val, QSql.ParamTypeFlag[] type)
addBindValue()
calls determines which placeholder a value will be bound to in the prepared query. If paramType is QSql::Out
or QSql::InOut
, the placeholder will be overwritten with data from the database after the exec()
call. To bind a NULL value, use a null QVariant
; for example, use QVariant(QVariant::String) if you are binding a string.
bindValue()
, prepare()
, exec()
, boundValue()
, and boundValues()
.
public final void addBindValue(java.lang.Object val)
addBindValue()
calls determines which placeholder a value will be bound to in the prepared query. If paramType is QSql::Out
or QSql::InOut
, the placeholder will be overwritten with data from the database after the exec()
call. To bind a NULL value, use a null QVariant
; for example, use QVariant(QVariant::String) if you are binding a string.
bindValue()
, prepare()
, exec()
, boundValue()
, and boundValues()
.
public final void addBindValue(java.lang.Object val, QSql.ParamType type)
addBindValue()
calls determines which placeholder a value will be bound to in the prepared query. If paramType is QSql::Out
or QSql::InOut
, the placeholder will be overwritten with data from the database after the exec()
call. To bind a NULL value, use a null QVariant
; for example, use QVariant(QVariant::String) if you are binding a string.
bindValue()
, prepare()
, exec()
, boundValue()
, and boundValues()
.
public final int at()
QSql::BeforeFirstRow
or QSql::AfterLastRow
, which are special negative values. previous()
, next()
, first()
, last()
, seek()
, isActive()
, and isValid()
.
public final void bindValue(java.lang.String placeholder, java.lang.Object val, QSql.ParamTypeFlag[] type)
QSql::Out
or QSql::InOut
, the placeholder will be overwritten with data from the database after the exec()
call. To bind a NULL value, use a null QVariant
; for example, use QVariant(QVariant::String) if you are binding a string.
addBindValue()
, prepare()
, exec()
, boundValue()
, and boundValues()
.
public final void bindValue(java.lang.String placeholder, java.lang.Object val)
QSql::Out
or QSql::InOut
, the placeholder will be overwritten with data from the database after the exec()
call. To bind a NULL value, use a null QVariant
; for example, use QVariant(QVariant::String) if you are binding a string.
addBindValue()
, prepare()
, exec()
, boundValue()
, and boundValues()
.
public final void bindValue(java.lang.String placeholder, java.lang.Object val, QSql.ParamType type)
QSql::Out
or QSql::InOut
, the placeholder will be overwritten with data from the database after the exec()
call. To bind a NULL value, use a null QVariant
; for example, use QVariant(QVariant::String) if you are binding a string.
addBindValue()
, prepare()
, exec()
, boundValue()
, and boundValues()
.
public final void bindValue(int pos, java.lang.Object val, QSql.ParamTypeFlag[] type)
QSql::Out
or QSql::InOut
, the placeholder will be overwritten with data from the database after the exec()
call.
public final void bindValue(int pos, java.lang.Object val)
QSql::Out
or QSql::InOut
, the placeholder will be overwritten with data from the database after the exec()
call.
public final void bindValue(int pos, java.lang.Object val, QSql.ParamType type)
QSql::Out
or QSql::InOut
, the placeholder will be overwritten with data from the database after the exec()
call.
public final java.lang.Object boundValue(java.lang.String placeholder)
boundValues()
, bindValue()
, and addBindValue()
.
public final java.lang.Object boundValue(int pos)
public final java.util.SortedMap boundValues()
With named binding, the bound values can be examined in the following ways:
Map<String, Object> map = query.boundValues(); for (String key : map.keySet()) { Object value = map.get(key); System.out.println(i.key().toAscii().data() << ": " << i.value().toString().toAscii().data()); }With positional binding, the code becomes:
int i = 0; for (Object value : query.boundValues().values()) System.err.println(String.valueOf(i++) + ": " + value);
boundValue()
, bindValue()
, and addBindValue()
.
public final void clear()
public final QSqlDriver driver()
public final boolean exec()
Note that the last error for this query is reset when exec()
is called.
prepare()
, bindValue()
, addBindValue()
, boundValue()
, and boundValues()
.
public final boolean exec(java.lang.String query)
After the query is executed, the query is positioned on an invalid record and must be navigated to a valid record before data values can be retrieved (for example, using next()
).
Note that the last error for this query is reset when exec()
is called.
Example:
QSqlQuery query = new QSqlQuery(); query.exec("INSERT INTO employee (id, name, salary) " + "VALUES (1001, 'Thad Beaumont', 65000)");
isActive()
, isValid()
, next()
, previous()
, first()
, last()
, and seek()
.
public final boolean execBatch()
exec()
calls. Returns true if the query is executed successfully; otherwise returns false.
Example:
QSqlQuery q = new QSqlQuery(); q.prepare("insert into myTable values (?, ?)"); List<Integer> ints = new ArrayList<Integer>(); ints.add(1); ints.add(2); ints.add(3); ints.add(4); q.addBindValue(ints); List<String> names = new ArrayList<String>(); names.add("Harald"); names.add("Boris"); names.add("Trond"); names.add(null); q.addBindValue(names); if (!q.execBatch()) System.out.println(q.lastError());The example above inserts four new rows into myTable:
1 Harald 2 Boris 3 Trond 4 NULLTo bind NULL values, a null
QVariant
of the relevant type has to be added to the bound QVariantList; for example, QVariant(QVariant::String) should be used if you are using strings. Note that every bound QVariantList must contain the same amount of variants. Note that the type of the QVariants in a list must not change. For example, you cannot mix integer and string variants within a QVariantList.
The mode parameter indicates how the bound QVariantList will be interpreted. If mode is ValuesAsRows, every variant within the QVariantList will be interpreted as a value for a new row. ValuesAsColumns is a special case for the Oracle driver. In this mode, every entry within a QVariantList will be interpreted as array-value for an IN or OUT value within a stored procedure. Note that this will only work if the IN or OUT value is a table-type consisting of only one column of a basic type, for example TYPE myType IS TABLE OF VARCHAR(64) INDEX BY BINARY_INTEGER;
prepare()
, bindValue()
, and addBindValue()
.
public final boolean execBatch(QSqlQuery.BatchExecutionMode mode)
exec()
calls. Returns true if the query is executed successfully; otherwise returns false.
Example:
QSqlQuery q = new QSqlQuery(); q.prepare("insert into myTable values (?, ?)"); List<Integer> ints = new ArrayList<Integer>(); ints.add(1); ints.add(2); ints.add(3); ints.add(4); q.addBindValue(ints); List<String> names = new ArrayList<String>(); names.add("Harald"); names.add("Boris"); names.add("Trond"); names.add(null); q.addBindValue(names); if (!q.execBatch()) System.out.println(q.lastError());The example above inserts four new rows into myTable:
1 Harald 2 Boris 3 Trond 4 NULLTo bind NULL values, a null
QVariant
of the relevant type has to be added to the bound QVariantList; for example, QVariant(QVariant::String) should be used if you are using strings. Note that every bound QVariantList must contain the same amount of variants. Note that the type of the QVariants in a list must not change. For example, you cannot mix integer and string variants within a QVariantList.
The mode parameter indicates how the bound QVariantList will be interpreted. If mode is ValuesAsRows, every variant within the QVariantList will be interpreted as a value for a new row. ValuesAsColumns is a special case for the Oracle driver. In this mode, every entry within a QVariantList will be interpreted as array-value for an IN or OUT value within a stored procedure. Note that this will only work if the IN or OUT value is a table-type consisting of only one column of a basic type, for example TYPE myType IS TABLE OF VARCHAR(64) INDEX BY BINARY_INTEGER;
prepare()
, bindValue()
, and addBindValue()
.
public final java.lang.String executedQuery()
In most cases this function returns the same string 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. It is mostly useful for debugging purposes.
lastQuery()
.
public final void finish()
Sets the query to inactive. Bound values retain their values.
prepare()
, exec()
, and isActive()
.
public final boolean first()
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. next()
, previous()
, last()
, seek()
, at()
, isActive()
, and isValid()
.
public final boolean isActive()
public final boolean isForwardOnly()
setForwardOnly()
, and next()
.
public final boolean isNull(int field)
isNull()
will not return accurate information until after an attempt is made to retrieve data. isActive()
, isValid()
, and value()
.
public final boolean isSelect()
public final boolean isValid()
public final boolean last()
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. next()
, previous()
, first()
, seek()
, at()
, isActive()
, and isValid()
.
public final QSqlError lastError()
QSqlError
, and QSqlDatabase::lastError()
.
public final java.lang.Object lastInsertId()
QVariant
will be returned if the query did not insert any value or if the database does not report the id back. If more than one row was touched by the insert, the behavior is undefined. For MySQL databases the row's auto-increment field will be returned.
Note: For this function to work in PSQL, the table table must contain OIDs, which may not have been created by default. Check the default_with_oids configuration variable to be sure.
QSqlDriver::hasFeature()
.
public final java.lang.String lastQuery()
executedQuery()
.
public final boolean next()
isSelect()
must return true before calling this function or it will do nothing and return false. The following rules apply:
previous()
, first()
, last()
, seek()
, at()
, isActive()
, and isValid()
.
public final boolean nextResult()
Some databases are capable of returning multiple result sets for stored procedures or SQL batches (a query strings that contains multiple statements). If multiple result sets are available after executing a query this function can be used to navigate to the next result set(s).
If a new result set is available this function will return true. The query will be repositioned on an invalid record in the new result set and must be navigated to a valid record before data values can be retrieved. If a new result set isn't available the function returns false and the the query is set to inactive. In any case the old result set will be discarded.
When one of the statements is a non-select statement a count of affected rows may be available instead of a result set.
Note that some databases, i.e. Microsoft SQL Server, requires non-scrollable cursors when working with multiple result sets. Some databases may execute all statements at once while others may delay the execution until the result set is actually accessed, and some databases may have restrictions on which statements are allowed to be used in a SQL batch.
QSqlDriver::hasFeature()
, setForwardOnly()
, next()
, isSelect()
, numRowsAffected()
, isActive()
, and lastError()
.
public final int numRowsAffected()
size()
instead. If the query is not active (isActive()
returns false), -1 is returned. size()
, and QSqlDriver::hasFeature()
.
public final QSql.NumericalPrecisionPolicy numericalPrecisionPolicy()
QSql::NumericalPrecisionPolicy
, and setNumericalPrecisionPolicy()
.
public final boolean prepare(java.lang.String query)
The query may contain placeholders for binding values. Both Oracle style colon-name (e.g., :surname), and ODBC style (?) placeholders are supported; but they cannot be mixed in the same query. See the Detailed Description
for examples.
Portability note: Some databases choose to delay preparing a query until it is executed the first time. In this case, preparing a syntactically wrong query succeeds, but every consecutive exec()
will fail.
Example:
QSqlQuery query = new QSqlQuery(); query.prepare("INSERT INTO person (id, forename, surname) " + "VALUES (:id, :forename, :surname)"); query.bindValue(":id", 1001); query.bindValue(":forename", "Bart"); query.bindValue(":surname", "Simpson"); query.exec();
exec()
, bindValue()
, and addBindValue()
.
public final boolean previous()
isSelect()
must return true before calling this function or it will do nothing and return false. The following rules apply:
next()
, first()
, last()
, seek()
, at()
, isActive()
, and isValid()
.
public final QSqlRecord record()
QSqlRecord
containing the field information for the current query. If the query points to a valid row (isValid()
returns true), the record is populated with the row's values. An empty record is returned when there is no active query (isActive()
returns false). To retrieve values from a query, value()
should be used since its index-based lookup is faster.
In the following example, a SELECT * FROM query is executed. Since the order of the columns is not defined, QSqlRecord::indexOf()
is used to obtain the index of a column.
QSqlQuery q = new QSqlQuery("select * from employees"); QSqlRecord rec = q.record(); System.out.println("Number of columns: " + rec.count()); int nameCol = rec.indexOf("name"); // index of the field "name" while (q.next()) System.out.println(q.value(nameCol).toString()); // output all names
value()
.
public final QSqlResult result()
public final boolean seek(int i)
isSelect()
must return true before calling this function. If relative is false (the default), the following rules apply:
next()
, previous()
, first()
, last()
, at()
, isActive()
, and isValid()
.
public final boolean seek(int i, boolean relative)
isSelect()
must return true before calling this function. If relative is false (the default), the following rules apply:
next()
, previous()
, first()
, last()
, at()
, isActive()
, and isValid()
.
public final void setForwardOnly(boolean forward)
next()
and seek()
with positive values, are allowed for navigating the results. Forward only mode can be (depending on the driver) more memory efficient since results do not need to be cached. It will also improve performance on some databases. For this to be true, you must call setForwardMode() before the query is prepared or executed. Note that the constructor that takes a query and a database may execute the query.
Forward only mode is off by default.
isForwardOnly()
, next()
, and seek()
.
public final void setNumericalPrecisionPolicy(QSql.NumericalPrecisionPolicy precisionPolicy)
The Oracle driver, for example, retrieves numerical values as strings by default to prevent the loss of precision. If the high precision doesn't matter, use this method to increase execution speed by bypassing string conversions.
Note: Drivers that don't support fetching numerical values with low precision will ignore the precision policy. You can use QSqlDriver::hasFeature()
to find out whether a driver supports this feature.
Note: Setting the precision policy doesn't affect the currently active query. Call exec(QString)
or prepare()
in order to activate the policy.
QSql::NumericalPrecisionPolicy
, and numericalPrecisionPolicy()
.
public final int size()
isSelect()
returns false), size()
will return -1. If the query is not active (isActive()
returns false), -1 is returned. To determine the number of rows affected by a non-SELECT statement, use numRowsAffected()
.
isActive()
, numRowsAffected()
, and QSqlDriver::hasFeature()
.
public final java.lang.Object value(int i)
The fields are numbered from left to right using the text of the SELECT statement, e.g. in
SELECT forename, surname FROM people;field 0 is forename and field 1 is surname. Using SELECT * is not recommended because the order of the fields in the query is undefined.
An invalid QVariant
is returned if field index does not exist, if the query is inactive, or if the query is positioned on an invalid record.
previous()
, next()
, first()
, last()
, seek()
, isActive()
, and isValid()
.
public QSqlQuery clone()
clone
in class java.lang.Object
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |