【问题标题】:Python using tkinter wont write to database使用 tkinter 的 Python 不会写入数据库
【发布时间】:2019-08-13 18:06:36
【问题描述】:

我创建了一个登录和注册系统,用户可以在其中输入他们的名字和姓氏、密码以及他们是教师还是学生,只需按一个按钮。但是,没有任何输入记录到数据库表中,并且最终会变为空,您可以通过将两个条目留空来登录到四个不同的帐户,因为我尝试的 4 次尝试都算作 4 个空帐户

def teachertype():
    var_usertype = ("Teacher")

def studenttype():
    var_usertype = ("Student")

def register():
    global screen2
    global var_FName
    global var_SName
    global var_password1
    global var_usertype
    var_FName = str()
    var_SName = str()
    var_password1 = str()
    var_usertype = str()

    screen2 = Toplevel(screen)
    screen2.title("Registration")
    screen2.geometry("500x500")
    Label(screen2, text = "Please enter your username and password below: ").pack()
    Label(screen2, text = "").pack()
    Label(screen2, text = "Firstname: ").pack()
    Entry(screen2, textvariable = var_FName).pack()
    Label(screen2, text = "Surname: ").pack()
    Entry(screen2, textvariable = var_SName).pack()
    Label(screen2, text = "").pack()
    Label(screen2, text = "Password: ").pack()
    Entry(screen2, textvariable = var_password1).pack()
    Label(screen2, text = "").pack()
    Button(screen2, text = "Teacher", width = 10, height = 1, command = teachertype).pack()
    Label(screen2, text = "").pack()
    Button(screen2, text = "Student", width = 10, height = 1, command = studenttype).pack()
    Label(screen2, text = "").pack()
    Button(screen2, text = "Register", width = 10, height = 1, command = regfunc).pack()

def regfunc():
    var_insert = []
    var_insert.append(var_FName)
    var_insert.append(var_SName)
    var_insert.append(str(var_password1))
    userID = var_FName[0:3] + var_SName
    var_insert.append(userID)
    var_insert.append(var_usertype)
    conn = sqlite3.connect('MyComputerScience.db')
    c = conn.cursor()
    c.execute('insert INTO users (FName, SName, password, username, userType)VALUES(?,?,?,?,?);', var_insert)
    conn.commit()
    Label(screen2, text = "Successfully registered! Your username to log in is "+userID+"", fg = "GREEN", font = "Calibri")
    login()

【问题讨论】:

    标签: python sqlite tkinter


    【解决方案1】:

    根据文档,var_insert 应该是一个元组而不是一个列表。 https://docs.python.org/2/library/sqlite3.html

    【讨论】:

      猜你喜欢
      • 2016-02-10
      • 2017-11-05
      • 2011-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多