InsertRow
-
wrote on 18 May 2021, 08:13 last edited by JonB
@Duy-Khang
At last! AQSqlQueryModel
is for querying SQL, it does not allow insertions, because it does not know where to insert any rows. It sounds like you should switch to aQSqlTableModel
for inserts etc.? I assume the rows you show are all in one table, which is where you want to insert/update/delete rows. -
wrote on 18 May 2021, 08:22 last edited by
oh,I don't know that before and how to insert new row if i use QSqlTableModel ?
-
wrote on 18 May 2021, 08:27 last edited by JonB
@Duy-Khang
The code you showed would work if the model is aQSqlTableModel
instead of aQSqlQueryModel
, and you have specified the table. You can search https://doc.qt.io/qt-5/qsqltablemodel.html and https://doc.qt.io/qt-5/qsqltablemodel-members.html forinsertRow
and see what you come across. There is alsoQSqlTableModel::insertRecord()
etc. if you prefer a record-based interface to a row-based interface, that is a matter of preference.BTW you don't need to worry about/implement the
beginInsertRows()
etc. you asked about if you use aQSqlTableModel
, its implementation handles that for you. -
wrote on 18 May 2021, 08:32 last edited by
and QSqlTableModel can not setQuery() how to do this ?
-
wrote on 18 May 2021, 08:41 last edited by
@Duy-Khang
Please read the documentation rather than asking each time. It is beneficial to you to understand, not just be told. There is even an example at https://doc.qt.io/qt-5/qsqltablemodel.html#details. -
wrote on 18 May 2021, 08:43 last edited by
Thank you very much
-
@Duy-Khang
Please read the documentation rather than asking each time. It is beneficial to you to understand, not just be told. There is even an example at https://doc.qt.io/qt-5/qsqltablemodel.html#details. -
@Duy-Khang said in InsertRow:
do you know how to disable or turn off the these indexs in this picture ?
Hide the horizontal and vertical header.
-
wrote on 18 May 2021, 17:58 last edited by JonB
@Duy-Khang
If that column is in the table but you don't want to show it, use void QTableView::hideColumn(int column) to suppress it in your view.Oh, I misunderstood what you were wanting to hide in your picture! I thought you meant the first column!
In which case it's the horizontal header as @Christian-Ehrlicher has pointed you to.
-
@Duy-Khang said in InsertRow:
do you know how to disable or turn off the these indexs in this picture ?
Hide the horizontal and vertical header.
wrote on 18 May 2021, 18:09 last edited by@Christian-Ehrlicher thank you.
-
@Duy-Khang
If that column is in the table but you don't want to show it, use void QTableView::hideColumn(int column) to suppress it in your view.Oh, I misunderstood what you were wanting to hide in your picture! I thought you meant the first column!
In which case it's the horizontal header as @Christian-Ehrlicher has pointed you to.
-
@Duy-Khang
If that column is in the table but you don't want to show it, use void QTableView::hideColumn(int column) to suppress it in your view.Oh, I misunderstood what you were wanting to hide in your picture! I thought you meant the first column!
In which case it's the horizontal header as @Christian-Ehrlicher has pointed you to.
-
What names do you want there ?
-
The same as usual.
As for setting the header data, it's shown in the QSqlTableModel documentation.
-
wrote on 19 May 2021, 19:06 last edited by
yes, i have done it and then how can I insert a row after the current row, assuming we don't have any rows at first, I use QSqlTableModel.
-
What issue do you have with using insertRow ?
-
yes, i have done it and then how can I insert a row after the current row, assuming we don't have any rows at first, I use QSqlTableModel.
wrote on 19 May 2021, 19:14 last edited by@Duy-Khang said in InsertRow:
how can I insert a row after the current row, assuming we don't have any rows at first
In addition to what @SGaist has been saying, how do you have a "current" row to insert after if you don't have any rows to start with?
-
wrote on 9 Nov 2022, 10:39 last edited by
import sys from PyQt5.QtWidgets import QWidget, QApplication,QTableView, QVBoxLayout,QPushButton from PyQt5.QtSql import QSqlDatabase, QSqlQuery, QSqlTableModel,QSqlRecord class demo(QWidget): def __init__(self): super().__init__() lyt = QVBoxLayout() self.setLayout(lyt) tview = QTableView() btn = QPushButton('add') lyt.addWidget(tview) lyt.addWidget(btn) db = QSqlDatabase.addDatabase("QSQLITE") db.setDatabaseName(r"xxx.db") self.model = testModel(db) tview.setModel(self.model) btn.clicked.connect(self.insertNewRecord) def insertNewRecord(self): r = QSqlRecord() r.setValue('name', 'hello') r.setValue('age', 23) if self.model.insertRecord(0, r): print(self.model.submitAll()) print(self.model.lastError().text()) else: self.model.db.rollback() print(self.model.lastError().text()) self.model.select() class testModel(QSqlTableModel): def __init__(self, db): super().__init__() self.db = db print(self.db.open()) self.setTable('test') self.setEditStrategy(QSqlTableModel.OnManualSubmit) print(self.select()) print(self.lastError().text()) print(self.tableName()) if __name__ == '__main__': app = QApplication(sys.argv) t = demo() t.show() sys.exit(app.exec())
data is displayed properly, but when i try to insertRecord, something wrong "No fields to update ". tableview not updated, let alone the database.
how to insert a record into the table?
-
import sys from PyQt5.QtWidgets import QWidget, QApplication,QTableView, QVBoxLayout,QPushButton from PyQt5.QtSql import QSqlDatabase, QSqlQuery, QSqlTableModel,QSqlRecord class demo(QWidget): def __init__(self): super().__init__() lyt = QVBoxLayout() self.setLayout(lyt) tview = QTableView() btn = QPushButton('add') lyt.addWidget(tview) lyt.addWidget(btn) db = QSqlDatabase.addDatabase("QSQLITE") db.setDatabaseName(r"xxx.db") self.model = testModel(db) tview.setModel(self.model) btn.clicked.connect(self.insertNewRecord) def insertNewRecord(self): r = QSqlRecord() r.setValue('name', 'hello') r.setValue('age', 23) if self.model.insertRecord(0, r): print(self.model.submitAll()) print(self.model.lastError().text()) else: self.model.db.rollback() print(self.model.lastError().text()) self.model.select() class testModel(QSqlTableModel): def __init__(self, db): super().__init__() self.db = db print(self.db.open()) self.setTable('test') self.setEditStrategy(QSqlTableModel.OnManualSubmit) print(self.select()) print(self.lastError().text()) print(self.tableName()) if __name__ == '__main__': app = QApplication(sys.argv) t = demo() t.show() sys.exit(app.exec())
data is displayed properly, but when i try to insertRecord, something wrong "No fields to update ". tableview not updated, let alone the database.
how to insert a record into the table?
wrote on 9 Nov 2022, 10:51 last edited by@ArthurPYQT
Since you callprint(self.model.lastError().text())
both wheninsertRecord(0, r)
succeeds and when it fails, do you want to tell us (actually prove) which route your code is following...?