将 QSqlRelationalTableModel 模型更改提交到数据库
Commit QSqlRelationalTableModel model changes to database
我修改了关系 table 模型
https://doc.qt.io/qt-5/qtsql-relationaltablemodel-relationaltablemodel-cpp.html
我希望能够将我的更改保存到我的数据库中,但它只保存前两列,而不保存主键查找行。另外,我无法删除这些行。我已经搜索过,但没有找到有关如何执行此操作的任何信息。我做错了什么?
我使用的示例数据是:
CREATE TABLE `category` ( `Id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `DrinkType` TEXT NOT NULL);
INSERT INTO `category` VALUES (1,'Non-alcoholic');
INSERT INTO `category` VALUES (2,'Alcoholic');
CREATE TABLE "drinks" ( `Id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `Name` TEXT NOT NULL, `Type` INTEGER );
INSERT INTO `drinks` VALUES (1,'Coca Cola',1);
INSERT INTO `drinks` VALUES (2,'Pilsner',2);
我的代码:
def addRecord():
model.insertRow(model.rowCount())
def delRecord():
model.removeRow(view.currentIndex().row())
model.select()
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window.setWindowTitle("QRelationalSqlTableModel")
con = QtSql.QSqlDatabase.addDatabase('QSQLITE')
con.setDatabaseName('drinks.db')
con.open()
model = QtSql.QSqlRelationalTableModel(parent = window)
model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
model. setTable('drinks')
model.setRelation(2, QtSql.QSqlRelation('category', 'id', 'drinktype'))
model.select()
model.setHeaderData(1, QtCore.Qt.Horizontal, 'Name')
model.setHeaderData(3, QtCore.Qt.Horizontal, 'Type of drink')
vbox = QtWidgets.QVBoxLayout()
view = QtWidgets.QTableView()
view.setModel(model)
view.setItemDelegate(QtSql.QSqlRelationalDelegate(view))
view.hideColumn(0)
view.setColumnWidth(1, 150)
view.setColumnWidth(2, 150)
vbox.addWidget(view)
btnAdd = QtWidgets.QPushButton("&Add record")
btnAdd.clicked.connect(addRecord)
vbox.addWidget(btnAdd)
btnDel = QtWidgets.QPushButton("&Delete record")
btnDel.clicked.connect(delRecord)
vbox.addWidget(btnDel)
window.setLayout(vbox)
window.resize(430, 250)
window.show()
sys.exit(app.exec_())
其中一个问题是因为您使用了不合适的策略,如果您使用 OnManualSubmit
您必须调用方法 submitAll()
以便将更改记录在数据库中,如果您不使用想要这样做,最好使用 OnRowChange
,使用此策略,将在添加压力值后输入或 select 另一行时进行更改。
如果你想从数据库中删除一行你必须使用 deleteRowFromTable()
,你正在使用 removeRow()
但这只会删除视图中的行而不是数据库中的行所以稍后数据刷新后显示新行。
def addRecord():
model.insertRow(model.rowCount())
view.scrollToBottom()
def delRecord():
model.deleteRowFromTable(view.currentIndex().row())
model.select()
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window.setWindowTitle("QRelationalSqlTableModel")
con = QtSql.QSqlDatabase.addDatabase('QSQLITE')
con.setDatabaseName('drinks.db')
con.open()
model = QtSql.QSqlRelationalTableModel(parent = window)
model.setEditStrategy(QtSql.QSqlTableModel.OnRowChange)
model. setTable('drinks')
model.setRelation(2, QtSql.QSqlRelation('category', 'id', 'drinktype'))
model.select()
model.setHeaderData(1, QtCore.Qt.Horizontal, 'Name')
model.setHeaderData(3, QtCore.Qt.Horizontal, 'Type of drink')
vbox = QtWidgets.QVBoxLayout()
view = QtWidgets.QTableView()
view.setModel(model)
view.setItemDelegate(QtSql.QSqlRelationalDelegate(view))
view.hideColumn(0)
view.setColumnWidth(1, 150)
view.setColumnWidth(2, 150)
vbox.addWidget(view)
btnAdd = QtWidgets.QPushButton("&Add record")
btnAdd.clicked.connect(addRecord)
vbox.addWidget(btnAdd)
btnDel = QtWidgets.QPushButton("&Delete record")
btnDel.clicked.connect(delRecord)
vbox.addWidget(btnDel)
window.setLayout(vbox)
window.resize(430, 250)
window.show()
sys.exit(app.exec_())
我修改了关系 table 模型 https://doc.qt.io/qt-5/qtsql-relationaltablemodel-relationaltablemodel-cpp.html
我希望能够将我的更改保存到我的数据库中,但它只保存前两列,而不保存主键查找行。另外,我无法删除这些行。我已经搜索过,但没有找到有关如何执行此操作的任何信息。我做错了什么?
我使用的示例数据是:
CREATE TABLE `category` ( `Id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `DrinkType` TEXT NOT NULL);
INSERT INTO `category` VALUES (1,'Non-alcoholic');
INSERT INTO `category` VALUES (2,'Alcoholic');
CREATE TABLE "drinks" ( `Id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `Name` TEXT NOT NULL, `Type` INTEGER );
INSERT INTO `drinks` VALUES (1,'Coca Cola',1);
INSERT INTO `drinks` VALUES (2,'Pilsner',2);
我的代码:
def addRecord():
model.insertRow(model.rowCount())
def delRecord():
model.removeRow(view.currentIndex().row())
model.select()
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window.setWindowTitle("QRelationalSqlTableModel")
con = QtSql.QSqlDatabase.addDatabase('QSQLITE')
con.setDatabaseName('drinks.db')
con.open()
model = QtSql.QSqlRelationalTableModel(parent = window)
model.setEditStrategy(QtSql.QSqlTableModel.OnManualSubmit)
model. setTable('drinks')
model.setRelation(2, QtSql.QSqlRelation('category', 'id', 'drinktype'))
model.select()
model.setHeaderData(1, QtCore.Qt.Horizontal, 'Name')
model.setHeaderData(3, QtCore.Qt.Horizontal, 'Type of drink')
vbox = QtWidgets.QVBoxLayout()
view = QtWidgets.QTableView()
view.setModel(model)
view.setItemDelegate(QtSql.QSqlRelationalDelegate(view))
view.hideColumn(0)
view.setColumnWidth(1, 150)
view.setColumnWidth(2, 150)
vbox.addWidget(view)
btnAdd = QtWidgets.QPushButton("&Add record")
btnAdd.clicked.connect(addRecord)
vbox.addWidget(btnAdd)
btnDel = QtWidgets.QPushButton("&Delete record")
btnDel.clicked.connect(delRecord)
vbox.addWidget(btnDel)
window.setLayout(vbox)
window.resize(430, 250)
window.show()
sys.exit(app.exec_())
其中一个问题是因为您使用了不合适的策略,如果您使用 OnManualSubmit
您必须调用方法 submitAll()
以便将更改记录在数据库中,如果您不使用想要这样做,最好使用 OnRowChange
,使用此策略,将在添加压力值后输入或 select 另一行时进行更改。
如果你想从数据库中删除一行你必须使用 deleteRowFromTable()
,你正在使用 removeRow()
但这只会删除视图中的行而不是数据库中的行所以稍后数据刷新后显示新行。
def addRecord():
model.insertRow(model.rowCount())
view.scrollToBottom()
def delRecord():
model.deleteRowFromTable(view.currentIndex().row())
model.select()
app = QtWidgets.QApplication(sys.argv)
window = QtWidgets.QWidget()
window.setWindowTitle("QRelationalSqlTableModel")
con = QtSql.QSqlDatabase.addDatabase('QSQLITE')
con.setDatabaseName('drinks.db')
con.open()
model = QtSql.QSqlRelationalTableModel(parent = window)
model.setEditStrategy(QtSql.QSqlTableModel.OnRowChange)
model. setTable('drinks')
model.setRelation(2, QtSql.QSqlRelation('category', 'id', 'drinktype'))
model.select()
model.setHeaderData(1, QtCore.Qt.Horizontal, 'Name')
model.setHeaderData(3, QtCore.Qt.Horizontal, 'Type of drink')
vbox = QtWidgets.QVBoxLayout()
view = QtWidgets.QTableView()
view.setModel(model)
view.setItemDelegate(QtSql.QSqlRelationalDelegate(view))
view.hideColumn(0)
view.setColumnWidth(1, 150)
view.setColumnWidth(2, 150)
vbox.addWidget(view)
btnAdd = QtWidgets.QPushButton("&Add record")
btnAdd.clicked.connect(addRecord)
vbox.addWidget(btnAdd)
btnDel = QtWidgets.QPushButton("&Delete record")
btnDel.clicked.connect(delRecord)
vbox.addWidget(btnDel)
window.setLayout(vbox)
window.resize(430, 250)
window.show()
sys.exit(app.exec_())