PYODBC错误07002 - 参数太少

时间:2016-03-09 13:13:57

标签: python sql database ms-access pyodbc

简单地说,此代码需要一定数量的库存项目,并通过从其中一个GUI元素添加数字来更新该值。每当我尝试运行以下代码时:

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()

我收到以下错误:

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)')

1 个答案:

答案 0 :(得分:3)

这里的问题是Python变量名UpdatedQuantity包含在SQL命令文本中,但数据库引擎并不知道这样的Python变量存在或者它的值可能是什么。该变量需要包含在传递给查询的参数中:

sql = 'update Stock set Current_Quantity = ? where Stock_Item = ?'
params = (
    UpdatedQuantity, 
    self.ui.Modify_Stock_Screen_Item.currentText()
    )
DBSelect.execute(sql, params)