QSqlQueryModelclass provides a read-only data model for SQL result sets. 更多 …
继承者: QSqlRelationalTableModel , QSqlTableModel
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()])
def
clear
()
def
indexInQuery
(item)
def
queryChange
()
QSqlQueryModelis a high-level interface for executing SQL statements and traversing the result set. It is built on top of the lower-levelQSqlQueryand can be used to provide data to view classes such asQTableView。例如:QSqlQueryModel *model = new QSqlQueryModel; model->setQuery("SELECT name, salary FROM employee"); model->setHeaderData(0, Qt::Horizontal, tr("Name")); model->setHeaderData(1, Qt::Horizontal, tr("Salary")); QTableView *view = new QTableView; view->setModel(model); view->show();We set the model’s query, then we set up the labels displayed in the view header.
QSqlQueryModelcan 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
salaryfield from record 4 in the result set of theSELECTquery. Sincesalaryis the 2nd column (or column index 1), we can rewrite the last line as follows:salary = model.data(model.index(4, 2))The model is read-only by default. To make it read-write, you must subclass it and reimplement
setData()andflags(). Another option is to useQSqlTableModel, which provides a read-write model based on a single database table.querymodel example illustrates how to use
QSqlQueryModelto display the result of a query. It also shows how to subclassQSqlQueryModelto customize the contents of the data before showing it to the user, and how to create a read-write model based onQSqlQueryModel.If the database doesn’t return the number of selected rows in a query, the model will fetch rows incrementally. See
fetchMore()了解更多信息。另请参阅
QSqlTableModelQSqlRelationalTableModelQSqlQuery模型/视图编程 Query Model Example
QSqlQueryModel
(
[
parent=None
]
)
¶
- param parent
QObject
Creates an empty
QSqlQueryModel
采用给定
parent
.
PySide2.QtSql.QSqlQueryModel.
beginInsertColumns
(
parent
,
first
,
last
)
¶
parent
–
QModelIndex
first
–
int
last
–
int
PySide2.QtSql.QSqlQueryModel.
beginInsertRows
(
parent
,
first
,
last
)
¶
parent
–
QModelIndex
first
–
int
last
–
int
PySide2.QtSql.QSqlQueryModel.
beginRemoveColumns
(
parent
,
first
,
last
)
¶
parent
–
QModelIndex
first
–
int
last
–
int
PySide2.QtSql.QSqlQueryModel.
beginRemoveRows
(
parent
,
first
,
last
)
¶
parent
–
QModelIndex
first
–
int
last
–
int
PySide2.QtSql.QSqlQueryModel.
beginResetModel
(
)
¶
PySide2.QtSql.QSqlQueryModel.
clear
(
)
¶
Clears the model and releases any acquired resource.
PySide2.QtSql.QSqlQueryModel.
endInsertColumns
(
)
¶
PySide2.QtSql.QSqlQueryModel.
endInsertRows
(
)
¶
PySide2.QtSql.QSqlQueryModel.
endRemoveColumns
(
)
¶
PySide2.QtSql.QSqlQueryModel.
endRemoveRows
(
)
¶
PySide2.QtSql.QSqlQueryModel.
endResetModel
(
)
¶
PySide2.QtSql.QSqlQueryModel.
indexInQuery
(
item
)
¶
item
–
QModelIndex
QModelIndex
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.
另请参阅
indexInQuery()
insertColumns()
removeColumns()
PySide2.QtSql.QSqlQueryModel.
lastError
(
)
¶
Returns information about the last error that occurred on the database.
另请参阅
PySide2.QtSql.QSqlQueryModel.
queryChange
(
)
¶
This virtual function is called whenever the query changes. The default implementation does nothing.
query()
returns the new query.
另请参阅
PySide2.QtSql.QSqlQueryModel.
record
(
)
¶
这是重载函数。
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.
另请参阅
PySide2.QtSql.QSqlQueryModel.
record
(
row
)
¶
row
–
int
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.
另请参阅
PySide2.QtSql.QSqlQueryModel.
setLastError
(
error
)
¶
error
–
QSqlError
Protected function which allows derived classes to set the value of the last error that occurred on the database to
error
.
另请参阅
PySide2.QtSql.QSqlQueryModel.
setQuery
(
query
)
¶
query
–
QSqlQuery
Resets the model and sets the data provider to be the given
query
. Note that the query must be active and must not be isForwardOnly().
lastError()
can be used to retrieve verbose information if there was an error setting the query.
注意
Calling will remove any inserted columns.
PySide2.QtSql.QSqlQueryModel.
setQuery
(
query
[
,
db=QSqlDatabase()
]
)
¶
query – unicode
db
–
QSqlDatabase
这是重载函数。
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.
范例:
model = QSqlQueryModel()
model.setQuery("select * from MyTable")
if model.lastError().isValid():
print model.lastError()