Variable problem in EventFilter
-
Hello
def eventFilter(self, obj, event): if obj == self.ui.tableWidget_2.viewport() and event.type() == QEvent.MouseButtonPress: if event.button() == Qt.RightButton: idx = self.ui.tableWidget_2.indexAt(event.pos()) if idx.isValid(): ajouterAction = QAction("Ajouter", self) ajouterAction.setObjectName(str(idx.row())) ajouterAction.triggered.connect(self.ajouter_action_triggered) contextMenu = QMenu(self) contextMenu.addAction(ajouterAction) contextMenu.exec(event.globalPos()) return QMainWindow.eventFilter(self, obj, event)
the fonction :
def ajouter_action_triggered(self): import os.path BASE_DIR = os.path.dirname(os.path.abspath(__file__)) db_path = os.path.join(BASE_DIR, "database_prix.db") with sqlite3.connect(db_path) as db: cursor=db.cursor() row = int(QObject.sender(self).objectName()) id = self.ui.tableWidget_2.item(row, 0).text() print(id) y=(str(id)) command = ''' SELECT PU.ref_pu, PU.desig_pu, PU.unite, PU.prix FROM PU WHERE id_pu = ? ''' result= cursor.execute(command,y) x = result.fetchone() print(x) self.ui.tableWidget_3.setItem(0,0,QtWidgets.QTableWidgetItem(str(x[0]))) self.ui.tableWidget_3.setItem(0,1,QtWidgets.QTableWidgetItem(str(x[1]))) self.ui.tableWidget_3.setItem(0,2,QtWidgets.QTableWidgetItem(str(x[2]))) self.ui.tableWidget_3.setItem(0,3,QtWidgets.QTableWidgetItem(str(x[3])))
To explain my problem to you, I want to add a row that belongs to one qtablewidget in another, I added the EventFilter function with event "AjouterAction" but each time I select another row in the first table they add value in the "y" variable and show me the error message:
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 3 supplied.
the query is good, everything is good except I don't understand why it stores in the variable "y" the old value and he adds to became 3 in this time.
If you dont understand my problem i can explain with an exemple. Thanks :)
-
@YassineKira Why don't you use https://doc.qt.io/qt-5/qsqldatabase.html instead of sqlite3?
-
@YassineKira
As @jsulm says, if you choose to use sqlite3 plus a load of Python code you might be better asking in forums dedicated to those.My Python is not perfect, but:
y=(str(id))
. I think there you are trying to create a list (or whatever it is called) of values here? If you only have a single value to put into that list don't you have to insert a trailing comma:y=(str(id), )
?You are re-opening and connecting to the database on each action click. Aren't you supposed to open/connect to the database once and re-use the connection as necessary?