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 。例如:
model = QSqlTableModel()
model.setTable("employee")
model.setEditStrategy(QSqlTableModel.OnManualSubmit)
model.select()
model.removeColumn(0) # don't show the ID
model.setHeaderData(0, Qt.Horizontal, tr("Name"))
model.setHeaderData(1, Qt.Horizontal, tr("Salary"))
view = QTableView()
view.setModel(model)
view.show()
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 ,和 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.
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.
另请参阅
PySide.QtSql.QSqlRelationalTableModel PySide.QtSql.QSqlQuery 模型/视图编程 表格模型范例 Cached Table Example
| 参数: |
|
|---|
Creates an empty PySide.QtSql.QSqlTableModel and sets the parent to parent and the database connection to db 。若 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.
| 常量 | 描述 |
|---|---|
| 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() 被调用。 |
Note: To prevent inserting only partly initialized rows into the database, OnFieldChange will behave like OnRowChange for newly inserted rows.
| 参数: | row – PySide.QtCore.int |
|---|
| 参数: | record – PySide.QtSql.QSqlRecord |
|---|
| 参数: |
|
|---|
| 返回类型: | PySide.QtSql.QSqlDatabase |
|---|
Returns a pointer to the used PySide.QtSql.QSqlDatabase or 0 if no database was set.
| 参数: | row – PySide.QtCore.int |
|---|---|
| 返回类型: | 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.
另请参阅
PySide.QtCore.QAbstractItemModel.removeRow() PySide.QtSql.QSqlTableModel.removeRows()
| 返回类型: | PySide.QtSql.QSqlTableModel.EditStrategy |
|---|
Returns the current edit strategy.
| 参数: | fieldName – unicode |
|---|---|
| 返回类型: | PySide.QtCore.int |
Returns the index of the field fieldName , or -1 if no corresponding field exists in the model.
| 返回类型: | unicode |
|---|
Returns the currently set filter.
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.bool |
插入 record after row 。若 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.
另请参阅
PySide.QtSql.QSqlTableModel.insertRows() PySide.QtSql.QSqlTableModel.removeRows()
| 参数: | values – PySide.QtSql.QSqlRecord |
|---|---|
| 返回类型: | 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() .
另请参阅
PySide.QtSql.QSqlQueryModel.lastError() PySide.QtCore.QAbstractItemModel.insertRow() PySide.QtSql.QSqlTableModel.insertRows()
| 参数: | index – PySide.QtCore.QModelIndex |
|---|---|
| 返回类型: | 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.
若 index is invalid or points to a non-existing row, false is returned.
| 返回类型: | unicode |
|---|
Returns an SQL ORDER BY clause based on the currently set sort order.
| 返回类型: | 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.
| 参数: |
|
|---|
Reverts all pending changes.
另请参阅
PySide.QtSql.QSqlTableModel.revert() PySide.QtSql.QSqlTableModel.revertRow() PySide.QtSql.QSqlTableModel.submitAll()
| 参数: | row – PySide.QtCore.int |
|---|
Reverts all changes for the specified row .
另请参阅
PySide.QtSql.QSqlTableModel.revert() PySide.QtSql.QSqlTableModel.revertAll() PySide.QtSql.QSqlTableModel.submit() PySide.QtSql.QSqlTableModel.submitAll()
| 返回类型: | 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.
注意
调用 PySide.QtSql.QSqlTableModel.select() will revert any unsubmitted changes and remove any inserted columns.
| 返回类型: | unicode |
|---|
Returns the SQL SELECT statement used internally to populate the model. The statement includes the filter and the ORDER BY 子句。
| 参数: | strategy – PySide.QtSql.QSqlTableModel.EditStrategy |
|---|
Sets the strategy for editing values in the database to strategy .
This will revert any pending changes.
| 参数: | 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() 被调用。
| 参数: | key – PySide.QtSql.QSqlIndex |
|---|
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.QtCore.bool |
Sets the values at the specified row to the values of record . Returns true if all the values could be set; otherwise returns false.
| 参数: |
|
|---|
| 参数: | 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() .
| 返回类型: | 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.
| 返回类型: | unicode |
|---|
Returns the name of the currently selected table.
| 参数: |
|
|---|---|
| 返回类型: |
PySide.QtCore.bool |
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() .
另请参阅
QSqlRecord.isGenerated() PySide.QtSql.QSqlTableModel.setData()