I'm trying to fill a QSQlQueryModel with data using setQuery and a specific database, but I get an error which to me looks like setQuery won't accept a string - despite that is what is described in documentation.
Im working with pyside6. Here's the code. It is redacted to the relevant lines.
from PySide6.QtWidgets import QApplication, QMainWindow, QTableWidgetItem
from PySide6 import QtSql, QtCore, QtGui, QtWidgets
from WiO_config import programmpfad #WiO is the name of my project. Config holds the path to the program ('Programmpfad') as str.
os.chdir(programmpfad)
import WiO_functions as WiO_func #WiO_func is a collection of functions I use in various forms
os.chdir(programmpfad + "Forms")
from Assessments.frmIndications import Ui_frmIndications
class Frm_Indications(QMainWindow, Ui_frmIndications):
def __init__(self):
super().__init__()
self.setupUi(self)
#Variables (there are more variables, but not relevant for my problem)
self.database_assess = 'path to desired datanbase'
self.query = 'qryIndication'
#Connection
self.db_assess = QtSql.QSqlDatabase.addDatabase("QSQLITE", self.database_assess)
self.db_assess.setDatabaseName(programmpfad + self.database_assess)
self.db_assess.open()
self.mod_Indication = QtSql.QSqlQueryModel #This is the model which should hold the data that is to be displayed in a QTableView later on,
#The troubles starts when I try to fill the model with setQuery(). This is initiated by calling Search()
def Search(self):
#create_WHERE() uses the lists 'Select...' and '_map' to construct an SQL WHERE-String: "datenfeld1 LIKE '%Inhalte Formularfeld1 %'..."
WHERE = WiO_func.create_WHERE(select_datafields, self.data_map, select_cbo, self.cbo_map, select_txt, self.txt_map)
self.mod_Indication.setQuery("SELECT * FROM qryIndication " + WHERE, db = self.db_assess)
And this ends up with the following error message instead of a model with data:
TypeError: descriptor 'setQuery' for 'PySide6.QtSql.QSqlQueryModel' objects doesn't apply to a 'str' object
What am I doing wrong?
In your code, you have self.mod_Indication = QtSql.QSqlQueryModel
. You need to add parenthesis to make it work correctly, like self.mod_Indication = QtSql.QSqlQueryModel()
.