QSqlTableModelclass provides an editable data model for a single database table. 更多 …
def
database
()
def
editStrategy
()
def
fieldIndex
(fieldName)
def
filter
()
def
insertRecord
(row, record)
def
isDirty
()
def
isDirty
(index)
def
primaryKey
()
def
primaryValues
(row)
def
setPrimaryKey
(key)
def
setRecord
(row, record)
def
tableName
()
def
deleteRowFromTable
(row)
def
insertRowIntoTable
(values)
def
orderByClause
()
def
revertRow
(row)
def
select
()
def
selectRow
(row)
def
selectStatement
()
def
setEditStrategy
(strategy)
def
setFilter
(filter)
def
setSort
(column, order)
def
setTable
(tableName)
def
updateRowInTable
(row, values)
def
beforeDelete
(row)
def
beforeInsert
(record)
def
beforeUpdate
(row, record)
def
primeInsert
(row, record)
QSqlTableModelis a high-level interface for reading and writing database records from a single table. It is built on top of the lower-levelQSqlQueryand can be used to provide data to view classes such asQTableView。例如:QSqlTableModel *model = new QSqlTableModel; model->setTable("employee"); model->setEditStrategy(QSqlTableModel::OnManualSubmit); model->select(); model->setHeaderData(0, Qt::Horizontal, tr("Name")); model->setHeaderData(1, Qt::Horizontal, tr("Salary")); QTableView *view = new QTableView; view->setModel(model); view->hideColumn(0); // don't show the ID 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.
QSqlTableModelcan also be used to access a database programmatically, without binding it to a view:model = QSqlTableModel() model.setTable("employee") name = model.record(4).value("name")The code snippet above extracts the
salaryfield from record 4 in the result set of the querySELECT * from employee.It is possible to set filters using
setFilter(), or modify the sort order usingsetSort(). At the end, you must callselect()to populate the model with data.tablemodel example illustrates how to use
QSqlTableModelas the data source for aQTableView.
QSqlTableModelprovides no direct support for foreign keys. Use theQSqlRelationalTableModelandQSqlRelationalDelegateif you want to resolve foreign keys.另请参阅
QSqlRelationalTableModelQSqlQuery模型/视图编程 表格模型范例 Cached Table Example
QSqlTableModel
(
[
parent=None
[
,
db=QSqlDatabase()
]
]
)
¶
- param parent
QObject- param db
Creates an empty
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
.
PySide2.QtSql.QSqlTableModel.
EditStrategy
¶
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
|
Note: To prevent inserting only partly initialized rows into the database,
OnFieldChange
will behave like
OnRowChange
for newly inserted rows.
另请参阅
PySide2.QtSql.QSqlTableModel.
beforeDelete
(
row
)
¶
row
–
int
PySide2.QtSql.QSqlTableModel.
beforeInsert
(
record
)
¶
record
–
QSqlRecord
PySide2.QtSql.QSqlTableModel.
beforeUpdate
(
row
,
record
)
¶
row
–
int
record
–
QSqlRecord
PySide2.QtSql.QSqlTableModel.
database
(
)
¶
Returns the model’s database connection.
PySide2.QtSql.QSqlTableModel.
deleteRowFromTable
(
row
)
¶
row
–
int
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
removeRow()
or
removeRows()
to delete values. The model will decide depending on its edit strategy when to modify the database.
返回
true
if the row was deleted; otherwise returns
false
.
另请参阅
removeRow()
removeRows()
PySide2.QtSql.QSqlTableModel.
editStrategy
(
)
¶
Returns the current edit strategy.
另请参阅
PySide2.QtSql.QSqlTableModel.
fieldIndex
(
fieldName
)
¶
fieldName – unicode
int
Returns the index of the field
fieldName
, or -1 if no corresponding field exists in the model.
PySide2.QtSql.QSqlTableModel.
filter
(
)
¶
unicode
Returns the currently set filter.
另请参阅
PySide2.QtSql.QSqlTableModel.
insertRecord
(
row
,
record
)
¶
row
–
int
record
–
QSqlRecord
bool
插入
record
在位置
row
。若
row
is negative, the record will be appended to the end. Calls
insertRows()
and
setRecord()
internally.
返回
true
if the record could be inserted, otherwise false.
Changes are submitted immediately for
OnFieldChange
and
OnRowChange
. Failure does not leave a new row in the model.
另请参阅
insertRows()
removeRows()
setRecord()
PySide2.QtSql.QSqlTableModel.
insertRowIntoTable
(
values
)
¶
values
–
QSqlRecord
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
insertRow()
and
setData()
to insert values. The model will decide depending on its edit strategy when to modify the database.
返回
true
if the values could be inserted, otherwise false. Error information can be retrieved with
lastError()
.
另请参阅
lastError()
insertRow()
insertRows()
PySide2.QtSql.QSqlTableModel.
isDirty
(
)
¶
bool
这是重载函数。
返回
true
if the model contains modified values that have not been committed to the database, otherwise false.
PySide2.QtSql.QSqlTableModel.
isDirty
(
index
)
¶
index
–
QModelIndex
bool
返回
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.
PySide2.QtSql.QSqlTableModel.
orderByClause
(
)
¶
unicode
Returns an SQL
ORDER
BY
clause based on the currently set sort order.
PySide2.QtSql.QSqlTableModel.
primaryKey
(
)
¶
Returns the primary key for the current table, or an empty
QSqlIndex
if the table is not set or has no primary key.
PySide2.QtSql.QSqlTableModel.
primaryValues
(
row
)
¶
row
–
int
Returns a record containing the fields represented in the primary key set to the values at
row
. If no primary key is defined, the returned record will contain all fields.
另请参阅
PySide2.QtSql.QSqlTableModel.
primeInsert
(
row
,
record
)
¶
row
–
int
record
–
QSqlRecord
PySide2.QtSql.QSqlTableModel.
revertAll
(
)
¶
Reverts all pending changes.
另请参阅
revert()
revertRow()
submitAll()
PySide2.QtSql.QSqlTableModel.
revertRow
(
row
)
¶
row
–
int
Reverts all changes for the specified
row
.
另请参阅
revert()
revertAll()
submit()
submitAll()
PySide2.QtSql.QSqlTableModel.
select
(
)
¶
bool
Populates the model with data from the table that was set via
setTable()
, using the specified filter and sort condition, and returns
true
若成功;否则返回
false
.
注意
Calling will revert any unsubmitted changes and remove any inserted columns.
PySide2.QtSql.QSqlTableModel.
selectRow
(
row
)
¶
row
–
int
bool
Refreshes
row
in the model with values from the database table row matching on primary key values. Without a primary key, all column values must match. If no matching row is found, the model will show an empty row.
返回
true
若成功;否则返回
false
.
另请参阅
PySide2.QtSql.QSqlTableModel.
selectStatement
(
)
¶
unicode
Returns the SQL
SELECT
statement used internally to populate the model. The statement includes the filter and the
ORDER
BY
子句。
另请参阅
PySide2.QtSql.QSqlTableModel.
setEditStrategy
(
strategy
)
¶
strategy
–
EditStrategy
Sets the strategy for editing values in the database to
strategy
.
This will revert any pending changes.
PySide2.QtSql.QSqlTableModel.
setFilter
(
filter
)
¶
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
select()
被调用。
PySide2.QtSql.QSqlTableModel.
setPrimaryKey
(
key
)
¶
key
–
QSqlIndex
Protected method that allows subclasses to set the primary key to
key
.
Normally, the primary index is set automatically whenever you call
setTable()
.
PySide2.QtSql.QSqlTableModel.
setRecord
(
row
,
record
)
¶
row
–
int
record
–
QSqlRecord
bool
Applies
values
到
row
in the model. The source and target fields are mapped by field name, not by position in the record.
Note that the generated flags in
values
are preserved to determine whether the corresponding fields are used when changes are submitted to the database. By default, it is set to
true
for all fields in a
QSqlRecord
. You must set the flag to
false
使用
setGenerated
(false) for any value in
values
, to save changes back to the database.
For edit strategies
OnFieldChange
and
OnRowChange
, a row may receive a change only if no other row has a cached change. Changes are submitted immediately. Submitted changes are not reverted upon failure.
返回
true
if all the values could be set; otherwise returns false.
另请参阅
record()
editStrategy()
PySide2.QtSql.QSqlTableModel.
setSort
(
column
,
order
)
¶
column
–
int
order
–
SortOrder
Sets the sort order for
column
to
order
. This does not affect the current data, to refresh the data using the new sort order, call
select()
.
另请参阅
sort()
select()
orderByClause()
PySide2.QtSql.QSqlTableModel.
setTable
(
tableName
)
¶
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
select()
.
Error information can be retrieved with
lastError()
.
PySide2.QtSql.QSqlTableModel.
submitAll
(
)
¶
bool
Submits all pending changes and returns
true
on success. Returns
false
on error, detailed error information can be obtained with
lastError()
.
在
OnManualSubmit
, 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 fails. This allows transactions to be rolled back and resubmitted without losing data.
另请参阅
PySide2.QtSql.QSqlTableModel.
tableName
(
)
¶
unicode
Returns the name of the currently selected table.
PySide2.QtSql.QSqlTableModel.
updateRowInTable
(
row
,
values
)
¶
row
–
int
values
–
QSqlRecord
bool
Updates the given
row
in the currently active database table with the specified
values
。返回
true
若成功;否则返回
false
.
This is a low-level method that operates directly on the database and should not be called directly. Use
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
setGenerated()
and tested with
isGenerated()
.
另请参阅
isGenerated()
setData()