- class QSqlResult#
The
QSqlResult
class provides an abstract interface for accessing data from specific SQL databases. More…Synopsis#
Methods#
def
__init__()
def
addBindValue()
def
at()
def
bindValueType()
def
bindingSyntax()
def
boundValue()
def
boundValueName()
def
clear()
def
driver()
def
exec_()
def
executedQuery()
def
hasOutValues()
def
isActive()
def
isForwardOnly()
def
isSelect()
def
isValid()
def
lastError()
def
lastQuery()
def
resetBindCount()
Virtual methods#
def
bindValue()
def
data()
def
exec()
def
execBatch()
def
fetch()
def
fetchFirst()
def
fetchLast()
def
fetchNext()
def
fetchPrevious()
def
handle()
def
isNull()
def
lastInsertId()
def
nextResult()
def
prepare()
def
record()
def
reset()
def
savePrepare()
def
setActive()
def
setAt()
def
setForwardOnly()
def
setLastError()
def
setQuery()
def
setSelect()
def
size()
Note
This documentation may contain snippets that were automatically translated from C++ to Python. We always welcome contributions to the snippet translation. If you see an issue with the translation, you can also let us know by creating a ticket on https:/bugreports.qt.io/projects/PYSIDE
Detailed Description#
Normally, you would use
QSqlQuery
instead ofQSqlResult
, sinceQSqlQuery
provides a generic wrapper for database-specific implementations ofQSqlResult
.If you are implementing your own SQL driver (by subclassing
QSqlDriver
), you will need to provide your ownQSqlResult
subclass that implements all the pure virtual functions and other virtual functions that you need.See also
- class BindingSyntax#
This enum type specifies the different syntaxes for specifying placeholders in prepared queries.
Constant
Description
QSqlResult.PositionalBinding
Use the ODBC-style positional syntax, with “?” as placeholders.
QSqlResult.NamedBinding
Use the Oracle-style syntax with named placeholders (e.g., “:id”)
See also
- class VirtualHookOperation#
- __init__(db)#
- Parameters:
db –
QSqlDriver
Creates a
QSqlResult
using database driverdb
. The object is initialized to an inactive state.See also
- addBindValue(val, type)#
- Parameters:
val – object
type – Combination of
ParamTypeFlag
Binds the value
val
of parameter typeparamType
to the next available position in the current record (row).See also
- at()#
- Return type:
int
Returns the current (zero-based) row position of the result. May return the special values
BeforeFirstRow
orAfterLastRow
.- bindValue(placeholder, val, type)#
- Parameters:
placeholder – str
val – object
type – Combination of
ParamTypeFlag
This is an overloaded function.
Binds the value
val
of parameter typeparamType
to theplaceholder
name in the current record (row).- bindValue(pos, val, type)
- Parameters:
pos – int
val – object
type – Combination of
ParamTypeFlag
Binds the value
val
of parameter typeparamType
to positionindex
in the current record (row).See also
- bindValueType(placeholder)#
- Parameters:
placeholder – str
- Return type:
Combination of
ParamTypeFlag
This is an overloaded function.
Returns the parameter type for the value bound with the given
placeholder
name.- bindValueType(pos)
- Parameters:
pos – int
- Return type:
Combination of
ParamTypeFlag
Returns the parameter type for the value bound at position
index
.See also
- bindingSyntax()#
- Return type:
Returns the binding syntax used by prepared queries.
- boundValue(placeholder)#
- Parameters:
placeholder – str
- Return type:
object
This is an overloaded function.
Returns the value bound by the given
placeholder
name in the current record (row).See also
- boundValue(pos)
- Parameters:
pos – int
- Return type:
object
Returns the value bound at position
index
in the current record (row).See also
bindValue()
boundValues()
- boundValueCount()#
- Return type:
int
Returns the number of bound values in the result.
See also
boundValues()
- boundValueName(pos)#
- Parameters:
pos – int
- Return type:
str
Returns the name of the bound value at position
index
in the current record (row).See also
- boundValueNames()#
- Return type:
list of strings
Returns the names of all bound values.
See also
- clear()#
Clears the entire result set and releases any associated resources.
- abstract data(i)#
- Parameters:
i – int
- Return type:
object
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 andindex
is non-negative. Derived classes must reimplement this function and return the value of fieldindex
, or QVariant() if it cannot be determined.- detachFromResultSet()#
- driver()#
- Return type:
Returns the driver associated with the result. This is the object that was passed to the constructor.
- exec()#
- Return type:
bool
Executes the query, returning true if successful; otherwise returns false.
See also
- execBatch([arrayBind=false])#
- Parameters:
arrayBind – bool
- Return type:
bool
- exec_()#
- Return type:
bool
- executedQuery()#
- Return type:
str
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.
See also
- abstract fetch(i)#
- Parameters:
i – int
- Return type:
bool
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
, and callsetAt()
with an appropriate value. Return true to indicate success, or false to signify failure.- abstract fetchFirst()#
- Return type:
bool
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.See also
- abstract fetchLast()#
- Return type:
bool
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.See also
- fetchNext()#
- Return type:
bool
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 callsetAt()
with an appropriate value. Return true to indicate success, or false to signify failure.See also
- fetchPrevious()#
- Return type:
bool
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 callsetAt()
with an appropriate value. Return true to indicate success, or false to signify failure.- handle()#
- Return type:
object
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
Returns the low-level database handle for this result set wrapped in a QVariant or an invalid QVariant if there is no handle.
Warning
Use this with uttermost care and only if you know what you’re doing.
Warning
The handle returned here can become a stale pointer if the result is modified (for example, if you clear it).
Warning
The handle can be NULL if the result was not executed yet.
Warning
PostgreSQL: in forward-only mode, the handle of
QSqlResult
can change after callingfetch()
,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:
db = QSqlDatabase.database("sales") query = QSqlQuery("SELECT NAME, DOB FROM EMPLOYEES", db) v = query.result().handle() if v.isValid() and qstrcmp(v.typeName(), "sqlite3_stmt*") == 0: # v.data() returns a pointer to the handle sqlite3_stmt handle = sqlite3_stmt(v.data()) if handle: # ...
This snippet returns the handle for PostgreSQL or MySQL:
if qstrcmp(v.typeName(), "PGresult*") == 0: handle = PGresult(v.data()) if handle: # ... if qstrcmp(v.typeName(), "MYSQL_STMT*") == 0: MYSQL_STMT handle = MYSQL_STMT(v.data()) if handle: # ...
See also
handle()
- hasOutValues()#
- Return type:
bool
Returns
true
if at least one of the query’s bound values is aQSql::Out
or aInOut
; otherwise returnsfalse
.See also
- isActive()#
- Return type:
bool
Returns
true
if the result has records to be retrieved; otherwise returnsfalse
.- isForwardOnly()#
- Return type:
bool
Returns
true
if you can only scroll forward through the result set; otherwise returnsfalse
.See also
- abstract isNull(i)#
- Parameters:
i – int
- Return type:
bool
Returns
true
if the field at positionindex
in the current row is null; otherwise returnsfalse
.- isPositionalBindingEnabled()#
- Return type:
bool
- isSelect()#
- Return type:
bool
Returns
true
if the current result is from aSELECT
statement; otherwise returnsfalse
.See also
- isValid()#
- Return type:
bool
Returns
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 returnsfalse
.See also
Returns the last error associated with the result.
See also
- lastInsertId()#
- Return type:
object
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.
See also
- lastQuery()#
- Return type:
str
Returns the current SQL query text, or an empty string if there isn’t one.
See also
- nextResult()#
- Return type:
bool
- abstract numRowsAffected()#
- Return type:
int
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
statement.See also
- numericalPrecisionPolicy()#
- Return type:
- prepare(query)#
- Parameters:
query – str
- Return type:
bool
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 returnsfalse
.See also
- record()#
- Return type:
Returns the current record if the query is active; otherwise returns an empty
QSqlRecord
.The default implementation always returns an empty
QSqlRecord
.See also
- abstract reset(sqlquery)#
- Parameters:
sqlquery – str
- Return type:
bool
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.See also
- resetBindCount()#
Resets the number of bind parameters.
- savePrepare(sqlquery)#
- Parameters:
sqlquery – str
- Return type:
bool
Prepares the given
query
, using the underlying database functionality where possible. Returnstrue
if the query is prepared successfully; otherwise returnsfalse
.Note: This method should have been called “safePrepare()”.
See also
- setActive(a)#
- Parameters:
a – bool
This function is provided for derived classes to set the internal active state to
active
.See also
- setAt(at)#
- Parameters:
at – int
This function is provided for derived classes to set the internal (zero-based) row position to
index
.See also
- setForwardOnly(forward)#
- Parameters:
forward – bool
Sets forward only mode to
forward
. Ifforward
is true, onlyfetchNext()
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.Note
Calling setForwardOnly after execution of the query will result in unexpected results at best, and crashes at worst.
Note
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.Warning
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.
See also
This function is provided for derived classes to set the last error to
error
.See also
- setNumericalPrecisionPolicy(policy)#
- Parameters:
policy –
NumericalPrecisionPolicy
- setPositionalBindingEnabled(enable)#
- Parameters:
enable – bool
- setQuery(query)#
- Parameters:
query – str
Sets the current query for the result to
query
. You must callreset()
to execute the query on the database.See also
- setSelect(s)#
- Parameters:
s – bool
This function is provided for derived classes to indicate whether or not the current statement is a SQL
SELECT
statement. Theselect
parameter should be true if the statement is aSELECT
statement; otherwise it should be false.See also
- abstract size()#
- Return type:
int
Returns the size of the
SELECT
result, or -1 if it cannot be determined or if the query is not aSELECT
statement.See also