QSqlResult Class

The QSqlResult class provides an abstract interface for accessing data from specific SQL databases. 更多...

头: #include <QSqlResult>
CMake: find_package(Qt6 REQUIRED COMPONENTS Sql)
target_link_libraries(mytarget PRIVATE Qt6::Sql)
qmake: QT += sql

公共函数

virtual ~QSqlResult ()
virtual QVariant handle () const

保护类型

enum BindingSyntax { PositionalBinding, NamedBinding }

保护函数

QSqlResult (const QSqlDriver * db )
void addBindValue (const QVariant & val , QSql::ParamType paramType )
int at () const
virtual void bindValue (int index , const QVariant & val , QSql::ParamType paramType )
virtual void bindValue (const QString & placeholder , const QVariant & val , QSql::ParamType paramType )
QSql::ParamType bindValueType (int index ) const
QSql::ParamType bindValueType (const QString & placeholder ) const
QSqlResult::BindingSyntax bindingSyntax () const
QVariant boundValue (int index ) const
QVariant boundValue (const QString & placeholder ) const
int boundValueCount () const
QString boundValueName (int index ) const
QList<QVariant> & boundValues () const
void clear ()
virtual QVariant data (int index ) = 0
const QSqlDriver * driver () const
virtual bool exec ()
QString executedQuery () const
virtual bool fetch (int index ) = 0
virtual bool fetchFirst () = 0
virtual bool fetchLast () = 0
virtual bool fetchNext ()
virtual bool fetchPrevious ()
bool hasOutValues () const
bool isActive () const
bool isForwardOnly () const
virtual bool isNull (int index ) = 0
bool isSelect () const
bool isValid () const
QSqlError lastError () const
virtual QVariant lastInsertId () const
QString lastQuery () const
virtual int numRowsAffected () = 0
virtual bool prepare (const QString & query )
virtual QSqlRecord record () const
virtual bool reset (const QString & query ) = 0
void resetBindCount ()
virtual bool savePrepare (const QString & query )
virtual void setActive (bool active )
virtual void setAt (int index )
virtual void setForwardOnly (bool forward )
virtual void setLastError (const QSqlError & error )
virtual void setQuery (const QString & query )
virtual void setSelect (bool select )
virtual int size () = 0

详细描述

Normally, you would use QSqlQuery instead of QSqlResult, since QSqlQuery provides a generic wrapper for database-specific implementations of QSqlResult.

If you are implementing your own SQL driver (by subclassing QSqlDriver ), you will need to provide your own QSqlResult subclass that implements all the pure virtual functions and other virtual functions that you need.

另请参阅 QSqlDriver .

成员类型文档编制

enum QSqlResult:: BindingSyntax

This enum type specifies the different syntaxes for specifying placeholders in prepared queries.

常量 描述
QSqlResult::PositionalBinding 0 Use the ODBC-style positional syntax, with "?" as placeholders.
QSqlResult::NamedBinding 1 Use the Oracle-style syntax with named placeholders (e.g., ":id")

另请参阅 bindingSyntax ().

成员函数文档编制

[explicit protected] QSqlResult:: QSqlResult (const QSqlDriver * db )

Creates a QSqlResult using database driver db . The object is initialized to an inactive state.

另请参阅 isActive () 和 driver ().

[虚拟] QSqlResult:: ~QSqlResult ()

销毁对象并释放任何分配资源。

[protected] void QSqlResult:: addBindValue (const QVariant & val , QSql::ParamType paramType )

Binds the value val of parameter type paramType to the next available position in the current record (row).

另请参阅 bindValue ().

[protected] int QSqlResult:: at () const

Returns the current (zero-based) row position of the result. May return the special values QSql::BeforeFirstRow or QSql::AfterLastRow .

另请参阅 setAt () 和 isValid ().

[virtual protected] void QSqlResult:: bindValue ( int index , const QVariant & val , QSql::ParamType paramType )

Binds the value val of parameter type paramType to position index in the current record (row).

另请参阅 addBindValue ().

[virtual protected] void QSqlResult:: bindValue (const QString & placeholder , const QVariant & val , QSql::ParamType paramType )

这是重载函数。

Binds the value val of parameter type paramTypeplaceholder name in the current record (row).

注意: Binding an undefined placeholder will result in undefined behavior.

另请参阅 QSqlQuery::bindValue ().

[protected] QSql::ParamType QSqlResult:: bindValueType ( int index ) const

Returns the parameter type for the value bound at position index .

另请参阅 boundValue ().

[protected] QSql::ParamType QSqlResult:: bindValueType (const QString & placeholder ) const

这是重载函数。

Returns the parameter type for the value bound with the given placeholder 名称。

[protected] QSqlResult::BindingSyntax QSqlResult:: bindingSyntax () const

Returns the binding syntax used by prepared queries.

[protected] QVariant QSqlResult:: boundValue ( int index ) const

Returns the value bound at position index in the current record (row).

另请参阅 bindValue () 和 boundValues ().

[protected] QVariant QSqlResult:: boundValue (const QString & placeholder ) const

这是重载函数。

Returns the value bound by the given placeholder name in the current record (row).

另请参阅 bindValueType ().

[protected] int QSqlResult:: boundValueCount () const

Returns the number of bound values in the result.

另请参阅 boundValues ().

[protected] QString QSqlResult:: boundValueName ( int index ) const

Returns the name of the bound value at position index in the current record (row).

另请参阅 boundValue ().

[protected] QList < QVariant > &QSqlResult:: boundValues () const

Returns a vector of the result's bound values for the current record (row).

另请参阅 boundValueCount ().

[protected] void QSqlResult:: clear ()

Clears the entire result set and releases any associated resources.

[pure virtual protected] QVariant QSqlResult:: data ( int index )

Returns the data for field index in the current row as a QVariant . This function is only called if the result is in an active state and is positioned on a valid record and index is non-negative. Derived classes must reimplement this function and return the value of field index , or QVariant() if it cannot be determined.

[protected] const QSqlDriver *QSqlResult:: driver () const

Returns the driver associated with the result. This is the object that was passed to the constructor.

[virtual protected] bool QSqlResult:: exec ()

Executes the query, returning true if successful; otherwise returns false.

另请参阅 prepare ().

[protected] QString QSqlResult:: executedQuery () const

Returns the query that was actually executed. This may differ from the query that was passed, for example if bound values were used with a prepared query and the underlying database doesn't support prepared queries.

另请参阅 exec () 和 setQuery ().

[pure virtual protected] bool QSqlResult:: fetch ( int index )

Positions the result to an arbitrary (zero-based) row index .

This function is only called if the result is in an active state. Derived classes must reimplement this function and position the result to the row index ,和调用 setAt () with an appropriate value. Return true to indicate success, or false to signify failure.

另请参阅 isActive (), fetchFirst (), fetchLast (), fetchNext (),和 fetchPrevious ().

[pure virtual protected] bool QSqlResult:: fetchFirst ()

Positions the result to the first record (row 0) in the result.

This function is only called if the result is in an active state. Derived classes must reimplement this function and position the result to the first record, and call setAt () with an appropriate value. Return true to indicate success, or false to signify failure.

另请参阅 fetch () 和 fetchLast ().

[pure virtual protected] bool QSqlResult:: fetchLast ()

Positions the result to the last record (last row) in the result.

This function is only called if the result is in an active state. Derived classes must reimplement this function and position the result to the last record, and call setAt () with an appropriate value. Return true to indicate success, or false to signify failure.

另请参阅 fetch () 和 fetchFirst ().

[virtual protected] bool QSqlResult:: fetchNext ()

Positions the result to the next available record (row) in the result.

This function is only called if the result is in an active state. The default implementation calls fetch () with the next index. Derived classes can reimplement this function and position the result to the next record in some other way, and call setAt () with an appropriate value. Return true to indicate success, or false to signify failure.

另请参阅 fetch () 和 fetchPrevious ().

[virtual protected] bool QSqlResult:: fetchPrevious ()

Positions the result to the previous record (row) in the result.

This function is only called if the result is in an active state. The default implementation calls fetch () with the previous index. Derived classes can reimplement this function and position the result to the next record in some other way, and call setAt () with an appropriate value. Return true to indicate success, or false to signify failure.

[虚拟] QVariant QSqlResult:: handle () const

Returns the low-level database handle for this result set wrapped in a QVariant or an invalid QVariant if there is no handle.

警告: Use this with uttermost care and only if you know what you're doing.

警告: The handle returned here can become a stale pointer if the result is modified (for example, if you clear it).

警告: The handle can be NULL if the result was not executed yet.

警告: PostgreSQL: in forward-only mode, the handle of QSqlResult can change after calling fetch (), fetchFirst (), fetchLast (), fetchNext (), fetchPrevious (), nextResult().

The handle returned here is database-dependent, you should query the type name of the variant before accessing it.

This example retrieves the handle for a sqlite result:

QSqlDatabase db = QSqlDatabase::database("sales");
QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
QVariant v = query.result()->handle();
if (v.isValid() && qstrcmp(v.typeName(), "sqlite3_stmt*") == 0) {
    // v.data() returns a pointer to the handle
    sqlite3_stmt *handle = *static_cast<sqlite3_stmt **>(v.data());
    if (handle) {
        // ...
    }
}
					

This snippet returns the handle for PostgreSQL or MySQL:

if (qstrcmp(v.typeName(), "PGresult*") == 0) {
    PGresult *handle = *static_cast<PGresult **>(v.data());
    if (handle) {
        // ...
        }
}
if (qstrcmp(v.typeName(), "MYSQL_STMT*") == 0) {
    MYSQL_STMT *handle = *static_cast<MYSQL_STMT **>(v.data());
    if (handle) {
        // ...
        }
    }
					

另请参阅 QSqlDriver::handle ().

[protected] bool QSqlResult:: hasOutValues () const

返回 true if at least one of the query's bound values is a QSql::Out QSql::InOut ;否则返回 false .

另请参阅 bindValueType ().

[protected] bool QSqlResult:: isActive () const

返回 true if the result has records to be retrieved; otherwise returns false .

[protected] bool QSqlResult:: isForwardOnly () const

返回 true if you can only scroll forward through the result set; otherwise returns false .

另请参阅 setForwardOnly ().

[pure virtual protected] bool QSqlResult:: isNull ( int index )

返回 true if the field at position index in the current row is null; otherwise returns false .

[protected] bool QSqlResult:: isSelect () const

返回 true if the current result is from a SELECT statement; otherwise returns false .

另请参阅 setSelect ().

[protected] bool QSqlResult:: isValid () const

返回 true if the result is positioned on a valid record (that is, the result is not positioned before the first or after the last record); otherwise returns false .

另请参阅 at ().

[protected] QSqlError QSqlResult:: lastError () const

Returns the last error associated with the result.

另请参阅 setLastError ().

[virtual protected] QVariant QSqlResult:: lastInsertId () const

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.

Note that for Oracle databases the row's ROWID will be returned, while for MySQL databases the row's auto-increment field will be returned.

另请参阅 QSqlDriver::hasFeature ().

[protected] QString QSqlResult:: lastQuery () const

Returns the current SQL query text, or an empty string if there isn't one.

另请参阅 setQuery ().

[pure virtual protected] int QSqlResult:: numRowsAffected ()

Returns the number of rows affected by the last query executed, or -1 if it cannot be determined or if the query is a SELECT 语句。

另请参阅 size ().

[virtual protected] bool QSqlResult:: prepare (const QString & query )

Prepares the given query for execution; the query will normally use placeholders so that it can be executed repeatedly. Returns true if the query is prepared successfully; otherwise returns false .

另请参阅 exec ().

[virtual protected] QSqlRecord QSqlResult:: record () const

Returns the current record if the query is active; otherwise returns an empty QSqlRecord .

The default implementation always returns an empty QSqlRecord .

另请参阅 isActive ().

[pure virtual protected] bool QSqlResult:: reset (const QString & query )

Sets the result to use the SQL statement query for subsequent data retrieval.

Derived classes must reimplement this function and apply the query to the database. This function is only called after the result is set to an inactive state and is positioned before the first record of the new result. Derived classes should return true if the query was successful and ready to be used, or false otherwise.

另请参阅 setQuery ().

[protected] void QSqlResult:: resetBindCount ()

Resets the number of bind parameters.

[virtual protected] bool QSqlResult:: savePrepare (const QString & query )

Prepares the given query , using the underlying database functionality where possible. Returns true if the query is prepared successfully; otherwise returns false .

Note: This method should have been called "safePrepare()".

另请参阅 prepare ().

[virtual protected] void QSqlResult:: setActive ( bool active )

This function is provided for derived classes to set the internal active state to active .

另请参阅 isActive ().

[virtual protected] void QSqlResult:: setAt ( int index )

This function is provided for derived classes to set the internal (zero-based) row position to index .

另请参阅 at ().

[virtual protected] void QSqlResult:: setForwardOnly ( bool forward )

Sets forward only mode to forward 。若 forward is true, only fetchNext () is allowed for navigating the results. Forward only mode needs much less memory since results do not have to be cached. By default, this feature is disabled.

Setting forward only to false is a suggestion to the database engine, which has the final say on whether a result set is forward only or scrollable. isForwardOnly () will always return the correct status of the result set.

注意: Calling setForwardOnly after execution of the query will result in unexpected results at best, and crashes at worst.

注意: To make sure the forward-only query completed successfully, the application should check lastError () for an error not only after executing the query, but also after navigating the query results.

警告: PostgreSQL: While navigating the query results in forward-only mode, do not execute any other SQL command on the same database connection. This will cause the query results to be lost.

另请参阅 isForwardOnly (), fetchNext (),和 QSqlQuery::setForwardOnly ().

[virtual protected] void QSqlResult:: setLastError (const QSqlError & error )

This function is provided for derived classes to set the last error to error .

另请参阅 lastError ().

[virtual protected] void QSqlResult:: setQuery (const QString & query )

Sets the current query for the result to query . You must call reset () to execute the query on the database.

另请参阅 reset () 和 lastQuery ().

[virtual protected] void QSqlResult:: setSelect ( bool select )

This function is provided for derived classes to indicate whether or not the current statement is a SQL SELECT statement. The select parameter should be true if the statement is a SELECT statement; otherwise it should be false.

另请参阅 isSelect ().

[pure virtual protected] int QSqlResult:: size ()

Returns the size of the SELECT result, or -1 if it cannot be determined or if the query is not a SELECT 语句。

另请参阅 numRowsAffected ().