Put data in TableView after SQl query
-
Hi,
I have a problem how to put data form sql query. Every query has got a different structure as is come from anothers tables.
I'm not sure that I have to use QStandardItemModel or/with QStandardItem and how to put query result into TableViewdef zestawienia (self): con= self.dlg.comboBox_6.currentText() global model model = QStandardItemModel() res=QStandardItem() if con == Zest[0]: sql2 = "SELECT * FROM ZARZADZANIE.ZAMOWIENIA_WG_STATUS_V" q=query.exec_(sql2) a=self.dlg.tableView_5.setModel(model)#i'm not sure it's need to use while query.next(): res=QStandardItem(mod.get('STATUS ZAMOWIENIA'))#the same- 'Status zamowienia' is the name of 1st attribute res.insertColumns(2,2) model.appendRow(res) self.dlg.tableView_5.show()#to final present data if con == Zest[1]: #2nd question to present difference plan22 = "SELECT * FROM ZARZADZANIE.ZAMOWIENIA where ROK_PLANOWANIA=2022" q=query.exec_(plan22) a=self.dlg.tableView_5.setModel(model) print (a) while query.next(): res=QStandardItem(mod.get('ID')) res.insertColumns(2,2) print (res) self.dlg.tableView_5.show()
Thanks for all helpful answers
-
I've tried sth by adding:
q=query.exec_(sql2) model.setQuery(q) a=self.dlg.tableView_5.setModel(model)
inspiring by this question in forum:
https://forum.qt.io/topic/102277/how-to-insert-data-to-table-from-another-table-sql-in-qt/8 -
@Karoluss96
You should not be usingQStandard...
for records from a SQL database. You should use eitherQSqlTableModel
orQSqlQueryModel
for the database query and aQTableView
with that as a model. Then it should display your received rows/columns. You will not need to useinsertColumn()
orappendRow()
either. the query should fill the model and the view should display what is in the model. -
I'm trying by using your option:
global model q= QSqlQueryModel() model = QTableView() res=QStandardItem() if con == Zest[0]: sql2 = "SELECT * FROM ZARZADZANIE.ZAMOWIENIA_WG_STATUS_V"#zapytanie nie działa :-( query.exec_(sql2) a=q.setQuery(sql2) self.dlg.tableView_5.setModel(a) while query.next(): a=res.setQuery(q) b=res.insertColumns(a) model.appendRow(b) self.dlg.tableView_5.show() if con == Zest[1]: plan22 = "SELECT * FROM ZARZADZANIE.ZAMOWIENIA where ROK_PLANOWANIA=2022" query.exec_(plan22) a=q.setQuery(plan22) self.dlg.tableView_5.setModel(a) print (a) while query.next(): a=model.setQuery(q) b=res.insertColumns(a) model.appendRow(b) self.dlg.tableView_5.show()
but nothing appears in Table View. Sth should I make wrong?
-
@Karoluss96 You should put more context - where is this code actually located? I guess some method?
The problem is probably that you are creating local variables which are destroyed as soon as they go out of scope. -
It's a part of function to put data from selected sql in combobox to table View.
Begins of it looks that:
def zestawienia (self): #obsługa zakładki zestawień con= self.dlg.comboBox_6.currentText() global model q= QSqlQueryModel() model = QTableView() res=QStandardItem() if con == Zest[0]: ...
It vision is easy- from combo box show SQL's anwser in TableView below
-
@Karoluss96 As I wrote: you are creating local variables, make them class members instead...
-
You mean by put some variables in definition of function - next to (self) in brackets?
Or put eg. ..setquery and insertColumns at the beginning before if loop? -
@Karoluss96 For example:
self.model = QTableView()
-
I've done:
self.q= QSqlQueryModel() self.model = QTableView() if con == Zest[0]: sql2 = "SELECT * FROM ZARZADZANIE.ZAMOWIENIA_WG_STATUS_V"#zapytanie nie działa :-( query.exec_(sql2) a=q.setQuery(sql2) self.dlg.tableView_5.setModel(a)
but i gains error in loof if: local variable 'q' referenced before assignment
-
@Karoluss96 said in Put data in TableView after SQl query:
local variable 'q' referenced before assignment
it is self.q now, not just q...
-
so where i have to put
self.model = QTableView()
as you suggested?
-
@Karoluss96 In the code you already posted!
def zestawienia (self): #obsługa zakładki zestawień con= self.dlg.comboBox_6.currentText() global model self.q= QSqlQueryModel() self.model = QTableView() res=QStandardItem() if con == Zest[0]:
-
@Karoluss96
You need to understand about things like variable scopes as a part of Python not Qt. Just saying.I see you still have a
QStandardItem
. I cannot imagine why. As I said, you simply fill your model from SQL and attach aQTableView
to it, that's it.And please do not name as
model
something which is aQTableView
, it's incredibly confusing. -
I changed assigning QTableView from model to table.
Then I put it intoself.dlg.tableView_5.setModel(table)
but it makes an error
TypeError: setModel(self, QAbstractItemModel): argument 1 has unexpected type 'QTableView'
-
@Karoluss96 said in Put data in TableView after SQl query:
but it makes an error
Would be nice if you would tell us what error it is...
-
I put the text of error in a previous post /\
-
@Karoluss96 The error is quite clear: you can't set QTableView (which is a view) as model...
What @JonB suggested is to use https://doc.qt.io/qt-6/qsqltablemodel.html or https://doc.qt.io/qt-6/qsqlquerymodel.html as model to get data from your SQL database and set that model in self.dlg.tableView_5