Inherited by: QSqlRelationalTableModel



Virtual functions



Detailed Description

The PySide.QtSql.QSqlTableModel class provides an editable data model for a single database table.

PySide.QtSql.QSqlTableModel is a high-level interface for reading and writing database records from a single table. It is build on top of the lower-level PySide.QtSql.QSqlQuery and can be used to provide data to view classes such as PySide.QtGui.QTableView . For example:

model =  QSqlTableModel()
model.removeColumn(0) # don't show the ID
model.setHeaderData(0, Qt.Horizontal, tr("Name"))
model.setHeaderData(1, Qt.Horizontal, tr("Salary"))

view =  QTableView()

We set the SQL table’s name and the edit strategy, then we set up the labels displayed in the view header. The edit strategy dictates when the changes done by the user in the view are actually applied to the database. The possible values are OnFieldChange , OnRowChange , and OnManualSubmit .

PySide.QtSql.QSqlTableModel can also be used to access a database programmatically, without binding it to a view:

model = QSqlQueryModel()
model.setQuery("SELECT * FROM employee")
salary = model.record(4).value("salary")

The code snippet above extracts the salary field from record 4 in the result set of the query SELECT * from employee .

It is possible to set filters using PySide.QtSql.QSqlTableModel.setFilter() , or modify the sort order using PySide.QtSql.QSqlTableModel.setSort() . At the end, you must call PySide.QtSql.QSqlTableModel.select() to populate the model with data.

The sql/tablemodel example illustrates how to use PySide.QtSql.QSqlTableModel as the data source for a PySide.QtGui.QTableView .

PySide.QtSql.QSqlTableModel provides no direct support for foreign keys. Use the PySide.QtSql.QSqlRelationalTableModel and PySide.QtSql.QSqlRelationalDelegate if you want to resolve foreign keys.

See also

PySide.QtSql.QSqlRelationalTableModel PySide.QtSql.QSqlQuery Model/View Programming Table Model Example Cached Table Example

class PySide.QtSql.QSqlTableModel([parent=None[, db=QSqlDatabase()]])

Creates an empty PySide.QtSql.QSqlTableModel and sets the parent to parent and the database connection to db . If db is not valid, the default database connection will be used.

The default edit strategy is OnRowChange .


This enum type describes which strategy to choose when editing values in the database.

Constant Description
QSqlTableModel.OnFieldChange All changes to the model will be applied immediately to the database.
QSqlTableModel.OnRowChange Changes to a row will be applied when the user selects a different row.
QSqlTableModel.OnManualSubmit All changes will be cached in the model until either PySide.QtSql.QSqlTableModel.submitAll() or PySide.QtSql.QSqlTableModel.revertAll() is called.

Note: To prevent inserting only partly initialized rows into the database, OnFieldChange will behave like OnRowChange for newly inserted rows.

PySide.QtSql.QSqlTableModel.beforeUpdate(row, record)
Return type:PySide.QtSql.QSqlDatabase

Returns a pointer to the used PySide.QtSql.QSqlDatabase or 0 if no database was set.

Return type:PySide.QtCore.bool

Deletes the given row from the currently active database table.

This is a low-level method that operates directly on the database and should not be called directly. Use PySide.QtCore.QAbstractItemModel.removeRow() or PySide.QtSql.QSqlTableModel.removeRows() to delete values. The model will decide depending on its edit strategy when to modify the database.

Returns true if the row was deleted; otherwise returns false.

See also

PySide.QtCore.QAbstractItemModel.removeRow() PySide.QtSql.QSqlTableModel.removeRows()

Return type:PySide.QtSql.QSqlTableModel.EditStrategy

Returns the current edit strategy.

Parameters:fieldName – unicode
Return type:PySide.QtCore.int

Returns the index of the field fieldName , or -1 if no corresponding field exists in the model.

Return type:unicode

Returns the currently set filter.

PySide.QtSql.QSqlTableModel.insertRecord(row, record)
Return type:


Inserts the record after row . If row is negative, the record will be appended to the end. Calls PySide.QtSql.QSqlTableModel.insertRows() and PySide.QtSql.QSqlTableModel.setRecord() internally.

Returns true if the row could be inserted, otherwise false.

See also

PySide.QtSql.QSqlTableModel.insertRows() PySide.QtSql.QSqlTableModel.removeRows()

Return type:PySide.QtCore.bool

Inserts the values values into the currently active database table.

This is a low-level method that operates directly on the database and should not be called directly. Use PySide.QtCore.QAbstractItemModel.insertRow() and PySide.QtSql.QSqlTableModel.setData() to insert values. The model will decide depending on its edit strategy when to modify the database.

Returns true if the values could be inserted, otherwise false. Error information can be retrieved with PySide.QtSql.QSqlQueryModel.lastError() .

See also

PySide.QtSql.QSqlQueryModel.lastError() PySide.QtCore.QAbstractItemModel.insertRow() PySide.QtSql.QSqlTableModel.insertRows()

Return type:PySide.QtCore.bool

Returns true if the value at the index index is dirty, otherwise false. Dirty values are values that were modified in the model but not yet written into the database.

If index is invalid or points to a non-existing row, false is returned.

Return type:unicode

Returns an SQL ORDER BY clause based on the currently set sort order.

Return type:PySide.QtSql.QSqlIndex

Returns the primary key for the current table, or an empty PySide.QtSql.QSqlIndex if the table is not set or has no primary key.

PySide.QtSql.QSqlTableModel.primeInsert(row, record)

Reverts all pending changes.


Reverts all changes for the specified row .

See also

PySide.QtSql.QSqlTableModel.revert() PySide.QtSql.QSqlTableModel.revertAll() PySide.QtSql.QSqlTableModel.submit() PySide.QtSql.QSqlTableModel.submitAll()

Return type:PySide.QtCore.bool

Populates the model with data from the table that was set via PySide.QtSql.QSqlTableModel.setTable() , using the specified filter and sort condition, and returns true if successful; otherwise returns false.


Calling PySide.QtSql.QSqlTableModel.select() will revert any unsubmitted changes and remove any inserted columns.

Return type:unicode

Returns the SQL SELECT statement used internally to populate the model. The statement includes the filter and the ORDER BY clause.


Sets the strategy for editing values in the database to strategy .

This will revert any pending changes.

Parameters:filter – unicode

Sets the current filter to filter .

The filter is a SQL WHERE clause without the keyword WHERE (for example, name='Josephine') .

If the model is already populated with data from a database, the model re-selects it with the new filter. Otherwise, the filter will be applied the next time PySide.QtSql.QSqlTableModel.select() is called.


Protected method that allows subclasses to set the primary key to key .

Normally, the primary index is set automatically whenever you call PySide.QtSql.QSqlTableModel.setTable() .

PySide.QtSql.QSqlTableModel.setRecord(row, record)
Return type:


Sets the values at the specified row to the values of record . Returns true if all the values could be set; otherwise returns false.

PySide.QtSql.QSqlTableModel.setSort(column, order)
  • columnPySide.QtCore.int
  • orderPySide.QtCore.Qt.SortOrder
Parameters:tableName – unicode

Sets the database table on which the model operates to tableName . Does not select data from the table, but fetches its field information.

To populate the model with the table’s data, call PySide.QtSql.QSqlTableModel.select() .

Error information can be retrieved with PySide.QtSql.QSqlQueryModel.lastError() .

Return type:PySide.QtCore.bool

Submits all pending changes and returns true on success. Returns false on error, detailed error information can be obtained with PySide.QtSql.QSqlQueryModel.lastError() .

On success the model will be repopulated. Any views presenting it will lose their selections.

Note: In OnManualSubmit mode, already submitted changes won’t be cleared from the cache when PySide.QtSql.QSqlTableModel.submitAll() fails. This allows transactions to be rolled back and resubmitted again without losing data.

Return type:unicode

Returns the name of the currently selected table.

PySide.QtSql.QSqlTableModel.updateRowInTable(row, values)
Return type:


Updates the given row in the currently active database table with the specified values . Returns true if successful; otherwise returns false.

This is a low-level method that operates directly on the database and should not be called directly. Use PySide.QtSql.QSqlTableModel.setData() to update values. The model will decide depending on its edit strategy when to modify the database.

Note that only values that have the generated-flag set are updated. The generated-flag can be set with QSqlRecord.setGenerated() and tested with QSqlRecord.isGenerated() .

See also

QSqlRecord.isGenerated() PySide.QtSql.QSqlTableModel.setData()