Python Execute()只接受2个参数(给定3个)

2024-10-01 15:29:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用以下代码将值插入SQLite数据库:

con.Execute('''UPDATE tblPlayers SET p_Level = ? WHERE p_Username= ? ''', (PlayerLevel,PlayerUsername))

这是执行函数:

^{pr2}$

我得到了一个错误:

con.Execute('''UPDATE tblPlayers SET p_Level = ? WHERE p_Username= ? ''', (PlayerLevel,PlayerUsername)) TypeError: Execute() takes exactly 2 arguments (3 given)


Tags: 函数代码数据库executesqliteusernameupdatewhere
2条回答

您的Execute()方法只接受两个参数,self和{}。self参数由Python提供给绑定方法,因此只有SQL参数的空间:

def Execute(self,SQL):

但是您使用一个附加参数调用了绑定方法,而不仅仅是一个SQL参数:

^{pr2}$

传入的元组值以及自动插入的self参数和SQL参数使三个。在

如果要支持SQL参数,则需要接受这些参数:

def Execute(self, SQL, params=()):
    self.__connection.execute(SQL, params)
    self.__connection.commit()

这一行表示您正在输入两个参数:

con.Execute('''UPDATE tblPlayers SET p_Level = ? WHERE p_Username= ? ''', (PlayerLevel,PlayerUsername))

将这2个参数添加到随实例自动传递的隐式self参数,现在有3个参数。在

调用时请保持1个参数,或者修改Execute的定义以容纳更多的1个参数。在

相关问题 更多 >

    热门问题