QSqlQuery Class Reference
|
Constant | Value | Description |
---|---|---|
QSqlQuery::ValuesAsRows | 0 | - Updates multiple rows. Treats every entry in a QVariantList as a value for updating the next row. |
QSqlQuery::ValuesAsColumns | 1 | - Updates a single row. Treats every entry in a QVariantList as a single value of an array type. |
Constructs a QSqlQuery object which uses the QSqlResult result to communicate with a database.
Constructs a 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.
See also QSqlDatabase.
Constructs a QSqlQuery object using the database db.
See also QSqlDatabase.
Constructs a copy of other.
Destroys the object and frees any allocated resources.
Adds the value val 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 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.
See also bindValue(), prepare(), exec(), boundValue(), and boundValues().
Returns the current internal position of the query. The first record is at position zero. If the position is invalid, the function returns QSql::BeforeFirstRow or QSql::AfterLastRow, which are special negative values.
See also previous(), next(), first(), last(), seek(), isActive(), and isValid().
Set the placeholder placeholder to be bound to value val in the prepared statement. Note that the placeholder mark (e.g :) must be included when specifying the placeholder name. 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.
See also addBindValue(), prepare(), exec(), boundValue(), and boundValues().
This is an overloaded member function, provided for convenience.
Set the placeholder in position pos to be bound to value val in the prepared statement. Field numbering starts at 0. If paramType is QSql::Out or QSql::InOut, the placeholder will be overwritten with data from the database after the exec() call.
Returns the value for the placeholder.
See also boundValues(), bindValue(), and addBindValue().
This is an overloaded member function, provided for convenience.
Returns the value for the placeholder at position pos.
Returns a map of the bound values.
With named binding, the bound values can be examined in the following ways:
QMapIterator<QString, QVariant> i(query.boundValues()); while (i.hasNext()) { i.next(); cout << i.key().toAscii().data() << ": " << i.value().toString().toAscii().data() << endl; }
With positional binding, the code becomes:
QList<QVariant> list = query.boundValues().values(); for (int i = 0; i < list.size(); ++i) cout << i << ": " << list.at(i).toString().toAscii().data() << endl;
See also boundValue(), bindValue(), and addBindValue().
Clears the result set and releases any resources held by the query. Sets the query state to inactive. You should rarely if ever need to call this function.
Returns the database driver associated with the query.
Executes the SQL in query. Returns true and sets the query state to active if the query was successful; otherwise returns false. The query string must use syntax appropriate for the SQL database being queried (for example, standard SQL).
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; query.exec("INSERT INTO employee (id, name, salary) " "VALUES (1001, 'Thad Beaumont', 65000)");
See also isActive(), isValid(), next(), previous(), first(), last(), and seek().
This is an overloaded member function, provided for convenience.
Executes a previously prepared SQL query. Returns true if the query executed successfully; otherwise returns false.
Note that the last error for this query is reset when exec() is called.
See also prepare(), bindValue(), addBindValue(), boundValue(), and boundValues().
Executes a previously prepared SQL query in a batch. All the bound parameters have to be lists of variants. If the database doesn't support batch executions, the driver will simulate it using conventional exec() calls.
Returns true if the query is executed successfully; otherwise returns false.
Example:
QSqlQuery q; q.prepare("insert into myTable values (?, ?)"); QVariantList ints; ints << 1 << 2 << 3 << 4; q.addBindValue(ints); QVariantList names; names << "Harald" << "Boris" << "Trond" << QVariant(QVariant::String); q.addBindValue(names); if (!q.execBatch()) qDebug() << q.lastError();
The example above inserts four new rows into myTable:
1 Harald 2 Boris 3 Trond 4 NULL
To 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: Every bound QVariantList must contain the same amount of variants.
Note: 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;
This function was introduced in Qt 4.2.
See also prepare(), bindValue(), and addBindValue().
Returns the last query that was successfully executed.
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.
See also lastQuery().
Instruct the database driver that no more data will be fetched from this query until it is re-executed. There is normally no need to call this function, but it may be helpful in order to free resources such as locks or cursors if you intend to re-use the query at a later time.
Sets the query to inactive. Bound values retain their values.
This function was introduced in Qt 4.3.2.
See also prepare(), exec(), and isActive().
Retrieves the first record in the result, if available, and positions the query on the retrieved record. Note that the result must be in the 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.
See also next(), previous(), last(), seek(), at(), isActive(), and isValid().
Returns true if the query is active. An active QSqlQuery is one that has been exec()'d successfully but not yet finished with. When you are finished with an active query, you can make make the query inactive by calling finish() or clear(), or you can delete the QSqlQuery instance.
Note: Of particular interest is an active query that is a SELECT statement. For some databases that support transactions, an active query that is a SELECT statement can cause a commit() or a rollback() to fail, so before committing or rolling back, you should make your active SELECT statement query inactive using one of the ways listed above.
See also isSelect().
Returns true if you can only scroll forward through a result set; otherwise returns false.
See also setForwardOnly() and next().
Returns true if the query is active and positioned on a valid record and the field 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.
See also isActive(), isValid(), and value().
Returns true if the current query is a SELECT statement; otherwise returns false.
Returns true if the query is currently positioned on a valid record; otherwise returns false.
Retrieves the last record in the result, if available, and positions the query on the retrieved record. Note that the result must be in the 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.
See also next(), previous(), first(), seek(), at(), isActive(), and isValid().
Returns error information about the last error (if any) that occurred with this query.
See also QSqlError and QSqlDatabase::lastError().
Returns the object ID of the most recent inserted row if the database supports it. An invalid 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.
See also QSqlDriver::hasFeature().
Returns the text of the current query being used, or an empty string if there is no current query text.
See also executedQuery().
Retrieves the next record in the result, if available, and positions the query on the retrieved record. Note that the result must be in the active state and isSelect() must return true before calling this function or it will do nothing and return false.
The following rules apply:
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.
See also previous(), first(), last(), seek(), at(), isActive(), and isValid().
Discards the current result set and navigates to the next if available.
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.
This function was introduced in Qt 4.4.
See also QSqlDriver::hasFeature(), setForwardOnly(), next(), isSelect(), numRowsAffected(), isActive(), and lastError().
Returns the number of rows affected by the result's SQL statement, or -1 if it cannot be determined. Note that for SELECT statements, the value is undefined; use size() instead. If the query is not active, -1 is returned.
See also size() and QSqlDriver::hasFeature().
Returns the current precision policy.
See also QSql::NumericalPrecisionPolicy and setNumericalPrecisionPolicy().
Prepares the SQL query query for execution. Returns true if the query is prepared successfully; otherwise returns false.
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; 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();
See also exec(), bindValue(), and addBindValue().
Retrieves the previous record in the result, if available, and positions the query on the retrieved record. Note that the result must be in the active state and isSelect() must return true before calling this function or it will do nothing and return false.
The following rules apply:
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.
See also next(), first(), last(), seek(), at(), isActive(), and isValid().
Returns a 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("select * from employees"); QSqlRecord rec = q.record(); qDebug() << "Number of columns: " << rec.count(); int nameCol = rec.indexOf("name"); // index of the field "name" while (q.next()) qDebug() << q.value(nameCol).toString(); // output all names
See also value().
Returns the result associated with the query.
Retrieves the record at position index, 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.
If relative is false (the default), the following rules apply:
If relative is true, the following rules apply:
See also next(), previous(), first(), last(), at(), isActive(), and isValid().
Sets forward only mode to forward. If forward is true, only 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.
See also isForwardOnly(), next(), and seek().
Instruct the database driver to return numerical values with a precision specified by 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.
See also QSql::NumericalPrecisionPolicy and numericalPrecisionPolicy().
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-SELECT statements (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().
See also isActive(), numRowsAffected(), and QSqlDriver::hasFeature().
Returns the value of field index in the current record.
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.
See also previous(), next(), first(), last(), seek(), isActive(), and isValid().
Assigns other to this object.
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt qtextended4.4 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com