QSqlQuery

Synopsis

Functions

Detailed Description

The PySide.QtSql.QSqlQuery class provides a means of executing and manipulating SQL statements.

PySide.QtSql.QSqlQuery encapsulates the functionality involved in creating, navigating and retrieving data from SQL queries which are executed on a PySide.QtSql.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 so that PySide.QtSql.QSqlQuery.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 PySide.QtSql.QSqlQuery.isValid() returns true) before values can be retrieved.

For some databases, if an active query that is a SELECT statement exists when you call PySide.QtSql.QSqlDatabase.commit() or PySide.QtSql.QSqlDatabase.rollback() , the commit or rollback will fail. See PySide.QtSql.QSqlQuery.isActive() for details.

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 PySide.QtSql.QSqlQuery.next() ), you can use PySide.QtSql.QSqlQuery.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 PySide.QtSql.QSqlQuery.value() . All data is transferred from the SQL backend using QVariants .

For example:

query = QSqlQuery("SELECT country FROM artist")
while query.next():
    country = query.value(0)
    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 PySide.QtSql.QSqlQuery.record() . PySide.QtSql.QSqlRecord.indexOf() , for example:

query = QSqlQuery("SELECT * FROM artist")
fieldNo = query.record().indexOf("country")
while query.next():
    country = query.value(fieldNo)
    doSomething(country)

PySide.QtSql.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 PySide.QtSql.QSqlQuery.numRowsAffected() to find out how many rows were affected by a non-SELECT query, and PySide.QtSql.QSqlQuery.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 PySide.QtSql.QSqlQuery.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.

Named binding using named placeholders:

query = 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:

query = 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):

query = 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):

query = 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:

This code calls a stored procedure called AsciiToInt() , passing it a character through its in parameter, and taking its result in the out parameter.

query = QSqlQuery()
query.prepare("CALL AsciiToInt(?, ?)")
query.bindValue(0, "A")
query.bindValue(1, 0, QSql.Out)
query.exec_()
i = query.boundValue(1) # i is 65

Note that unbound parameters will retain their values.

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 PySide.QtSql.QSqlQuery is created. Also, the connection must remain open while the query exists; otherwise, the behavior of PySide.QtSql.QSqlQuery is undefined.

class PySide.QtSql.QSqlQuery(db)
class PySide.QtSql.QSqlQuery(r)
class PySide.QtSql.QSqlQuery(other)
class PySide.QtSql.QSqlQuery([query=""[, db=QSqlDatabase()]])
Parameters:

Constructs a PySide.QtSql.QSqlQuery object using the database db . If db is invalid, the application’s default database will be used.

Constructs a PySide.QtSql.QSqlQuery object which uses the PySide.QtSql.QSqlResult result to communicate with a database.

Constructs a copy of other .

Constructs a PySide.QtSql.QSqlQuery object using the SQL query and the database db . If db is not specified, or is invalid, the application’s default database is used. If query is not an empty string, it will be executed.

PySide.QtSql.QSqlQuery.BatchExecutionMode
Constant Description
QSqlQuery.ValuesAsRows
  • Updates multiple rows. Treats every entry in a QVariantList as a value for updating the next row.
QSqlQuery.ValuesAsColumns
  • Updates a single row. Treats every entry in a QVariantList as a single value of an array type.
PySide.QtSql.QSqlQuery.addBindValue(val[, type=QSql.In])
Parameters:
  • val – object
  • typePySide.QtSql.QSql.ParamType
PySide.QtSql.QSqlQuery.at()
Return type:PySide.QtCore.int

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.

PySide.QtSql.QSqlQuery.bindValue(placeholder, val[, type=QSql.In])
Parameters:
  • placeholder – unicode
  • val – object
  • typePySide.QtSql.QSql.ParamType
PySide.QtSql.QSqlQuery.bindValue(pos, val[, type=QSql.In])
Parameters:
  • posPySide.QtCore.int
  • val – object
  • typePySide.QtSql.QSql.ParamType
PySide.QtSql.QSqlQuery.boundValue(placeholder)
Parameters:placeholder – unicode
Return type:object

Returns the value for the placeholder .

PySide.QtSql.QSqlQuery.boundValue(pos)
Parameters:posPySide.QtCore.int
Return type:object

Returns the value for the placeholder at position pos .

PySide.QtSql.QSqlQuery.boundValues()
Return type:

Returns a map of the bound values.

With named binding, the bound values can be examined in the following ways:

i = query.boundValues()
while i.hasNext():
    i.next()
    print i.key(), ": ", i.value()

With positional binding, the code becomes:

list_ = query.boundValues().values()
for item in list:
    print item
PySide.QtSql.QSqlQuery.clear()

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.

PySide.QtSql.QSqlQuery.driver()
Return type:PySide.QtSql.QSqlDriver

Returns the database driver associated with the query.

PySide.QtSql.QSqlQuery.execBatch([mode=ValuesAsRows])
Parameters:modePySide.QtSql.QSqlQuery.BatchExecutionMode
Return type:PySide.QtCore.bool

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:

q = QSqlQuery()
q.prepare("insert into myTable values (?, ?)")

ints = [1, 2, 3, 4]
q.addBindValue(ints)

names = ["Harald", "Boris", "Trond", ""]
q.addBindValue(names)

if not q.execBatch():
    print 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 PySide.QtCore.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;

PySide.QtSql.QSqlQuery.exec_(query)
Parameters:query – unicode
Return type:PySide.QtCore.bool

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 PySide.QtSql.QSqlQuery.next() ).

Note that the last error for this query is reset when exec() is called.

For SQLite, the query string can contain only one statement at a time. If more than one statements is give, the function returns false.

Example:

query = QSqlQuery()
query.exec_("INSERT INTO employee (id, name, salary) "
            "VALUES (1001, 'Thad Beaumont', 65000)")
PySide.QtSql.QSqlQuery.exec_()
Return type:PySide.QtCore.bool

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.

PySide.QtSql.QSqlQuery.executedQuery()
Return type:unicode

Returns the last query that was successfully executed.

In most cases this function returns the same string as PySide.QtSql.QSqlQuery.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.

PySide.QtSql.QSqlQuery.finish()

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.

PySide.QtSql.QSqlQuery.first()
Return type:PySide.QtCore.bool

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 PySide.QtSql.QSqlQuery.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.

PySide.QtSql.QSqlQuery.isActive()
Return type:PySide.QtCore.bool

Returns true if the query is active . An active PySide.QtSql.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 PySide.QtSql.QSqlQuery.finish() or PySide.QtSql.QSqlQuery.clear() , or you can delete the PySide.QtSql.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 PySide.QtSql.QSqlDatabase.commit() or a PySide.QtSql.QSqlDatabase.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.

PySide.QtSql.QSqlQuery.isForwardOnly()
Return type:PySide.QtCore.bool

Returns true if you can only scroll forward through a result set; otherwise returns false.

PySide.QtSql.QSqlQuery.isNull(field)
Parameters:fieldPySide.QtCore.int
Return type:PySide.QtCore.bool

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, PySide.QtSql.QSqlQuery.isNull() will not return accurate information until after an attempt is made to retrieve data.

PySide.QtSql.QSqlQuery.isSelect()
Return type:PySide.QtCore.bool

Returns true if the current query is a SELECT statement; otherwise returns false.

PySide.QtSql.QSqlQuery.isValid()
Return type:PySide.QtCore.bool

Returns true if the query is currently positioned on a valid record; otherwise returns false.

PySide.QtSql.QSqlQuery.last()
Return type:PySide.QtCore.bool

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 PySide.QtSql.QSqlQuery.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.

PySide.QtSql.QSqlQuery.lastError()
Return type:PySide.QtSql.QSqlError

Returns error information about the last error (if any) that occurred with this query.

PySide.QtSql.QSqlQuery.lastInsertId()
Return type:object

Returns the object ID of the most recent inserted row if the database supports it. An invalid PySide.QtCore.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.

PySide.QtSql.QSqlQuery.lastQuery()
Return type:unicode

Returns the text of the current query being used, or an empty string if there is no current query text.

PySide.QtSql.QSqlQuery.next()
Return type:PySide.QtCore.bool

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 PySide.QtSql.QSqlQuery.isSelect() must return true before calling this function or it will do nothing and return false.

The following rules apply:

  • 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.
  • If the result is currently located after the last record, there is no change and false is returned.
  • If the result is located somewhere in the middle, an attempt is made to retrieve the next record.

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.

PySide.QtSql.QSqlQuery.nextResult()
Return type:PySide.QtCore.bool

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 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.

PySide.QtSql.QSqlQuery.numRowsAffected()
Return type:PySide.QtCore.int

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 PySide.QtSql.QSqlQuery.size() instead. If the query is not active , -1 is returned.

PySide.QtSql.QSqlQuery.numericalPrecisionPolicy()
Return type:PySide.QtSql.QSql.NumericalPrecisionPolicy

Returns the current precision policy.

See also

QSql.NumericalPrecisionPolicy PySide.QtSql.QSqlQuery.setNumericalPrecisionPolicy()

PySide.QtSql.QSqlQuery.prepare(query)
Parameters:query – unicode
Return type:PySide.QtCore.bool

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.

For SQLite, the query string can contain only one statement at a time. If more than one statements are give, the function returns false.

Example:

query = 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_()
PySide.QtSql.QSqlQuery.previous()
Return type:PySide.QtCore.bool

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 PySide.QtSql.QSqlQuery.isSelect() must return true before calling this function or it will do nothing and return false.

The following rules apply:

  • If the result is currently located before the first record, there is no change and false is returned.
  • If the result is currently located after the last record, an attempt is made to retrieve the last record.
  • If the result is somewhere in the middle, an attempt is made to retrieve the previous record.

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.

PySide.QtSql.QSqlQuery.record()
Return type:PySide.QtSql.QSqlRecord

Returns a PySide.QtSql.QSqlRecord containing the field information for the current query. If the query points to a valid row ( PySide.QtSql.QSqlQuery.isValid() returns true), the record is populated with the row’s values. An empty record is returned when there is no active query ( PySide.QtSql.QSqlQuery.isActive() returns false).

To retrieve values from a query, PySide.QtSql.QSqlQuery.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.

q = QSqlQuery("select * from employees")
rec = q.record()

print "Number of columns: %d" % rec.count()

nameCol = rec.indexOf("name") # index of the field "name"
while q.next():
    print q.value(nameCol) # output all names
PySide.QtSql.QSqlQuery.result()
Return type:PySide.QtSql.QSqlResult

Returns the result associated with the query.

PySide.QtSql.QSqlQuery.seek(i[, relative=false])
Parameters:
  • iPySide.QtCore.int
  • relativePySide.QtCore.bool
Return type:

PySide.QtCore.bool

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 PySide.QtSql.QSqlQuery.isSelect() must return true before calling this function.

If relative is false (the default), the following rules apply:

  • If index is negative, the result is positioned before the first record and false is returned.
  • Otherwise, an attempt is made to move to the record at position index. If the record at position index 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.

If relative is true, the following rules apply:

  • If the result is currently positioned before the first record or on the first record, and index is negative, there is no change, and false is returned.
  • If the result is currently located after the last record, and index is positive, there is no change, and false is returned.
  • If the result is currently located somewhere in the middle, and the relative offset index moves the result below zero, the result is positioned before the first record and false is returned.
  • Otherwise, an attempt is made to move to the record index records ahead of the current record (or index records behind the current record if index is negative). If the record at offset index could not be retrieved, the result is positioned after the last record if index >= 0, (or before the first record if index is negative), and false is returned. If the record is successfully retrieved, true is returned.
PySide.QtSql.QSqlQuery.setForwardOnly(forward)
Parameters:forwardPySide.QtCore.bool

Sets forward only mode to forward . If forward is true, only PySide.QtSql.QSqlQuery.next() and PySide.QtSql.QSqlQuery.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 setForwardOnly() 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.

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. PySide.QtSql.QSqlQuery.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.

PySide.QtSql.QSqlQuery.setNumericalPrecisionPolicy(precisionPolicy)
Parameters:precisionPolicyPySide.QtSql.QSql.NumericalPrecisionPolicy
PySide.QtSql.QSqlQuery.size()
Return type:PySide.QtCore.int

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 ( PySide.QtSql.QSqlQuery.isSelect() returns false), PySide.QtSql.QSqlQuery.size() will return -1. If the query is not active ( PySide.QtSql.QSqlQuery.isActive() returns false), -1 is returned.

To determine the number of rows affected by a non-SELECT statement, use PySide.QtSql.QSqlQuery.numRowsAffected() .

PySide.QtSql.QSqlQuery.value(i)
Parameters:iPySide.QtCore.int
Return type:object

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 PySide.QtCore.QVariant is returned if field index does not exist, if the query is inactive, or if the query is positioned on an invalid record.