【问题标题】:sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 7, and there are 1 suppliedsqlite3.ProgrammingError:提供的绑定数量不正确。当前语句使用 7,并且提供了 1
【发布时间】:2020-03-26 05:05:20
【问题描述】:

我目前正在tkintersqlite3 开发健身房会员系统。我有这个持续的错误,我不知道出了什么问题。我无法弄清楚为什么提供的绑定数量不正确。

def new_user(self):
    #Establish Connection
    with sqlite3.connect('Gym.db') as db:
        c = db.cursor()

    #Find Existing username if an action is taken

    find_user = ('SELECT * FROM member WHERE username = ?')
    c.execute(find_user,[(self.n_Username.get())])
    if c.fetchall():
        ms.showerror('Error!','Username Taken Try a Diffrent One.')

    else:
        ms.showinfo('Success!','Account Created!')
        self.main_page()

    sql = "INSERT INTO 'Member' (FirstName,LastName,Email,Phone,Balance,Username,Password) VALUES(?,?,?,?,?,?,?)" #<-------
    c.execute(sql,[(self.n_FirstName.get(),self.n_LastName.get(),self.n_Email.get(),self.n_Phone.get(),self.n_Balance.get(),self.n_Username.get(),self.n_Password.get())])
    db.commit()

错误:

sqlite3.ProgrammingError:提供的绑定数量不正确。当前语句使用 7,提供了 1 个

【问题讨论】:

    标签: python mysql sqlite


    【解决方案1】:

    您正在提供一个包含一个元组的列表作为execute 方法的第二个参数。这会导致“提供 1”的问题,因为列表中的元组仅算作一个绑定。

    相反,删除列表并简单地传递包含的元组。即

    c.execute(sql,
              [(self.n_FirstName.get(), 
                self.n_LastName.get(),
                self.n_Email.get(),
                self.n_Phone.get(),
                self.n_Balance.get(),
                self.n_Username.get(),
                self.n_Password.get()
              )]
    )
    

    应该是:

    c.execute(sql,
              (self.n_FirstName.get(), 
               self.n_LastName.get(),
               self.n_Email.get(),
               self.n_Phone.get(),
               self.n_Balance.get(),
               self.n_Username.get(),
               self.n_Password.get()
              )
    )
    

    【讨论】:

      【解决方案2】:

      更正您的代码,如下所示: c.execute(find_user,(self.n_Username.get(),))

      删除“[]”并添加一个“,”。逗号也很重要,因为第二个参数应该是一个元组。

      【讨论】:

        猜你喜欢
        • 2021-10-28
        • 1970-01-01
        • 2020-09-08
        • 1970-01-01
        • 2020-09-10
        • 2013-05-27
        • 2020-10-15
        • 2016-01-21
        相关资源
        最近更新 更多