【问题标题】:Issue with username and password variables in sqlite3 and tkintersqlite3 和 tkinter 中的用户名和密码变量问题
【发布时间】:2019-08-11 14:00:28
【问题描述】:

我需要我的程序从 Tkinter 窗口的输入字段中获取输入并通过数据库运行它们以使用户登录,但是,它说我的参数 0(用户名)存在绑定问题,并且我假设我的密码变量也会发生这种情况。

def loginfunc():
        while True:
            with sqlite3.connect('MyComputerScience.db') as db:
                cursor = db.cursor()
            find_user = ("SELECT * FROM users WHERE username = ? AND password = ?")
            cursor.execute(find_user, (username, password))
            results = cursor.fetchall()
            if results:
                for i in results:
                    print ("welcome "+i[1]+"")
                    break
            else:
                print ("Username or password incorrect")
                login()
            break
def login():
        screen1 = Toplevel(screen)
        screen1.title("Login")
        screen1.geometry("300x250")

        global username
        global password
        username = StringVar()
        password = StringVar()

        Label(screen1, text = "Please enter your username and password below: ").pack()
        Label(screen1, text = "").pack()
        Label(screen1, text = "Username: ").pack()
        Entry(screen1, textvariable = username).pack()
        Label(screen1, text = "").pack()
        Label(screen1, text = "Password: ").pack()
        Entry(screen1, textvariable = password).pack()
        Label(screen1, text = "").pack()
        Button(screen1, text = "Login", width = 10, height = 1, command=loginfunc).pack()
File "C:\Users\notmyname\Desktop\NEA PROPER.py", line 90, in loginfunc
     cursor.execute(find_user, (username, password))
     sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.

【问题讨论】:

    标签: python sqlite tkinter


    【解决方案1】:

    正如错误所说,您的绑定参数属于不受支持的类型。 usernamepassword 的数据类型是 StringVar,这是一个 Tkinter(TCL) 内置类型。

    StringVar 不直接使用它来提供值,您需要在 StringVar 对象上调用 get 方法 以获取字符串形式的值,然后您可以将其传递给 cursor.execute 调用。

    Refer here to know about StringVar get method

    试试这个:

    cursor.execute(find_user, (username.get(), password.get()))
    

    【讨论】:

      猜你喜欢
      • 2023-02-26
      • 2018-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-02
      • 2013-05-07
      • 2013-09-07
      相关资源
      最近更新 更多