PYODBC error 07002 - Too few parameters

Viewed 8119

Put simply, this code takes a quantity of a stock item,and updates the value by adding a number from one of the GUI elements. Whenever I try to run the code below:

Change = self.ui.Modify_Stock_Screen_Change.text()
Change = int(Change)
DBConnect = pyodbc.connect('Driver={Microsoft Access Driver (*.mdb, *.accdb)}; Dbq=H:\\Computer Science\\X\\Y\\Z\\Database.accdb;')
DBSelect = DBConnect.cursor()
DBSelect.execute("select * from Stock where Stock_Item = ?", self.ui.Modify_Stock_Screen_Item.currentText())
TableRow= DBSelect.fetchone()
for Field in TableRow:
    CurrentQuantity = TableRow[1]
    CurrentQuantity = int(CurrentQuantity)
    UpdatedQuantity = int(CurrentQuantity + Change)
    DBSelect.execute('update Stock set Current_Quantity = UpdatedQuantity where Stock_Item = ?', self.ui.Modify_Stock_Screen_Item.currentText())
    DBConnect.commit()

I get the following error:

DBSelect.execute('update Stock set Current_Quantity = "UpdatedQuantity" where Stock_Item = ?', self.ui.Modify_Stock_Screen_Item.currentText())
pyodbc.Error: ('07002', '[07002] [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2. (-3010) (SQLExecDirectW)')
2 Answers

Problem is about quotes. Like in example below:

Good version:

curs.execute(f"select name, surname from employee where occupation='{param}'")

Bad version:

curs.execute(f"select name, surname from employee where occupation={param}")

Select or update, it doesn't matter.

Related