【问题标题】:Python Execute() takes exactly 2 arguments (3 given)Python Execute() 正好需要 2 个参数(给定 3 个)
【发布时间】:2015-01-29 17:17:08
【问题描述】:

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

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

这是执行函数:

def Execute(self,SQL):
    self.__connection.execute(SQL)
    self.__connection.comit()

我收到了这个错误:

con.Execute('''UPDATE tblPlayers SET p_Level = ? WHERE p_Username= ? ''', (PlayerLevel,PlayerUsername)) TypeError: Execute() 需要 2 个参数(3 个给定)

【问题讨论】:

  • 什么? ....你需要con.execute case 很重要...我根本不知道 Execute 函数...不是普通的 sqlite3 db_cursor?
  • 我基于 sqlite 创建了自己的 DB 类,使其更易于使用

标签: python sql sqlite python-2.x execute


【解决方案1】:

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

def Execute(self,SQL):

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

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

传入的元组值与自动插入的self 参数和SQL 参数一起构成三个

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

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

【讨论】:

    【解决方案2】:

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

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

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

    调用时要么保留 1 个参数,要么修改 Execute 的定义以容纳更多参数。

    【讨论】:

      猜你喜欢
      • 2013-04-07
      • 2016-09-01
      • 1970-01-01
      • 2012-03-15
      • 2014-05-06
      • 1970-01-01
      • 1970-01-01
      • 2013-07-26
      • 1970-01-01
      相关资源
      最近更新 更多