sqlite3和tkinter中的用户名和密码变量出现问题

时间:2019-08-11 14:00:28

标签: python sqlite tkinter

我需要我的程序从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.

1 个答案:

答案 0 :(得分:1)

错误显示您的绑定参数是不受支持的类型。 用户名密码的数据类型为 StringVar ,它是Tkinter(TCL)内置类型。

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

Refer here to know about StringVar get method

尝试一下:

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