QSqlQueryModel#
The QSqlQueryModel
class provides a read-only data model for SQL result sets. More…
Inherited by: QSqlTableModel, QSqlRelationalTableModel
Synopsis#
Functions#
def
beginInsertColumns
(parent, first, last)def
beginInsertRows
(parent, first, last)def
beginRemoveColumns
(parent, first, last)def
beginRemoveRows
(parent, first, last)def
beginResetModel
()def
endInsertColumns
()def
endInsertRows
()def
endRemoveColumns
()def
endRemoveRows
()def
endResetModel
()def
lastError
()def
query
()def
record
()def
record
(row)def
setLastError
(error)def
setQuery
(query)def
setQuery
(query[, db=QSqlDatabase()])
Virtual functions#
def
clear
()def
indexInQuery
(item)def
queryChange
()
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#
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
QSqlQueryModel
is a high-level interface for executing SQL statements and traversing the result set. It is built on top of the lower-level QSqlQuery
and can be used to provide data to view classes such as QTableView. For example:
model = QSqlQueryModel() model.setQuery("SELECT name, salary FROM employee") model.setHeaderData(0, Qt.Horizontal, tr("Name")) model.setHeaderData(1, Qt.Horizontal, tr("Salary")) view = QTableView() view.setModel(model) view.show()
We set the model’s query, then we set up the labels displayed in the view header.
QSqlQueryModel
can also be used to access a database programmatically, without binding it to a view:
model = QSqlQueryModel() model.setQuery("SELECT name, salary FROM employee") salary = model.record(4).value("salary").toInt()
The code snippet above extracts the salary
field from record 4 in the result set of the SELECT
query. Since salary
is the 2nd column (or column index 1), we can rewrite the last line as follows:
salary = model.data(model.index(4, 1)).toInt()
The model is read-only by default. To make it read-write, you must subclass it and reimplement setData() and flags(). Another option is to use QSqlTableModel
, which provides a read-write model based on a single database table.
The querymodel example illustrates how to use QSqlQueryModel
to display the result of a query. It also shows how to subclass QSqlQueryModel
to customize the contents of the data before showing it to the user, and how to create a read-write model based on QSqlQueryModel
.
If the database doesn’t return the number of selected rows in a query, the model will fetch rows incrementally. See fetchMore()
for more information.
- class PySide6.QtSql.QSqlQueryModel([parent=None])#
- Parameters:
parent –
PySide6.QtCore.QObject
Creates an empty QSqlQueryModel
with the given parent
.
- PySide6.QtSql.QSqlQueryModel.beginInsertColumns(parent, first, last)#
- Parameters:
parent –
PySide6.QtCore.QModelIndex
first – int
last – int
- PySide6.QtSql.QSqlQueryModel.beginInsertRows(parent, first, last)#
- Parameters:
parent –
PySide6.QtCore.QModelIndex
first – int
last – int
- PySide6.QtSql.QSqlQueryModel.beginRemoveColumns(parent, first, last)#
- Parameters:
parent –
PySide6.QtCore.QModelIndex
first – int
last – int
- PySide6.QtSql.QSqlQueryModel.beginRemoveRows(parent, first, last)#
- Parameters:
parent –
PySide6.QtCore.QModelIndex
first – int
last – int
- PySide6.QtSql.QSqlQueryModel.beginResetModel()#
- PySide6.QtSql.QSqlQueryModel.clear()#
Clears the model and releases any acquired resource.
- PySide6.QtSql.QSqlQueryModel.endInsertColumns()#
- PySide6.QtSql.QSqlQueryModel.endInsertRows()#
- PySide6.QtSql.QSqlQueryModel.endRemoveColumns()#
- PySide6.QtSql.QSqlQueryModel.endRemoveRows()#
- PySide6.QtSql.QSqlQueryModel.endResetModel()#
- PySide6.QtSql.QSqlQueryModel.indexInQuery(item)#
- Parameters:
item –
PySide6.QtCore.QModelIndex
- Return type:
Returns the index of the value in the database result set for the given item
in the model.
The return value is identical to item
if no columns or rows have been inserted, removed, or moved around.
Returns an invalid model index if item
is out of bounds or if item
does not point to a value in the result set.
See also
indexInQuery()
insertColumns()
removeColumns()
- PySide6.QtSql.QSqlQueryModel.lastError()#
- Return type:
Returns information about the last error that occurred on the database.
See also
- PySide6.QtSql.QSqlQueryModel.query()#
- Return type:
- PySide6.QtSql.QSqlQueryModel.queryChange()#
This virtual function is called whenever the query changes. The default implementation does nothing.
query()
returns the new query.
See also
- PySide6.QtSql.QSqlQueryModel.record()#
- Return type:
This is an overloaded function.
Returns an empty record containing information about the fields of the current query.
If the model is not initialized, an empty record will be returned.
See also
- PySide6.QtSql.QSqlQueryModel.record(row)
- Parameters:
row – int
- Return type:
Returns the record containing information about the fields of the current query. If row
is the index of a valid row, the record will be populated with values from that row.
If the model is not initialized, an empty record will be returned.
See also
- PySide6.QtSql.QSqlQueryModel.setLastError(error)#
- Parameters:
error –
PySide6.QtSql.QSqlError
Protected function which allows derived classes to set the value of the last error that occurred on the database to error
.
See also
- PySide6.QtSql.QSqlQueryModel.setQuery(query)#
- Parameters:
query –
PySide6.QtSql.QSqlQuery
Use the setQuery(QSqlQuery &&query)
overload instead.
This is an overloaded function.
- PySide6.QtSql.QSqlQueryModel.setQuery(query[, db=QSqlDatabase()])
- Parameters:
query – str
Warning
This section contains snippets that were automatically translated from C++ to Python and may contain errors.
This is an overloaded function.
Executes the query query
for the given database connection db
. If no database (or an invalid database) is specified, the default connection is used.
lastError()
can be used to retrieve verbose information if there was an error setting the query.
Example:
model = QSqlQueryModel() model.setQuery("select * from MyTable") if model.lastError().isValid(): print(model.lastError())See also