使用变量Python sqlite更新表中的记录

时间:2017-05-10 04:41:52

标签: python sql database sqlite

def CheckUpdateUser(self, usernameC, passwordC, usernameE, passwordE, edit_account_window):
    usernameC = usernameC.get()
    passwordC = passwordC.get()
    usernameE = usernameE.get()
    passwordE = passwordE.get()
    if len(usernameC) == 0:
        messagebox.showinfo("Error", "Please Enter Current Credentials. \n\nIf issue persists contact an Admin or try again.")
    elif len(usernameE) == 0:
        messagebox.showinfo("Error", "Please Enter New Credentials. \n\nIf issue persists contact an Admin or try again.")
    else:
        with sqlite3.connect("library.db") as db:
            cursor = db.cursor()
            cursor.execute("SELECT password from loginUsers WHERE username = ? and password = ?", (usernameC, passwordC))
            username_search = cursor.fetchone()
            if not username_search is None:
               # self.updateUserInDatabase
                cursor.execute("UPDATE loginUsers SET username = ? and password = ? WHERE username = ?", (usernameE, passwordE, usernameC))                      
                messagebox.showinfo("Success!", "Credentials updated, you can now go back to login page and use the credentials you entered")        
            else:
                messagebox.showinfo("Error.", "User Not Found. \n\nContact an Admin to register an account or try again.")

遇到问题:

        with sqlite3.connect("library.db") as db:
            cursor = db.cursor()
            cursor.execute("SELECT password from loginUsers WHERE username = ? and password = ?", (usernameC, passwordC))
            username_search = cursor.fetchone()
            if not username_search is None:
               # self.updateUserInDatabase
                cursor.execute("UPDATE loginUsers SET username = ? and password = ? WHERE username = ?", (usernameE, passwordE, usernameC))                      
                messagebox.showinfo("Success!", "Credentials updated, you can now go back to login page and use the credentials you entered")        

部分,我在Python中使用tkinter,我试图用新的用户信息(用户名和密码(usernameE,passwordE)更新当前用户信息(用户名和密码(usernameC,passwordC))。

usernameC和passwordC都是用户在代码的其他部分输入的内容:

    Label(edit_account_window, text='Current Username: ', bg = self.bgc, fg = "grey", font = "Arial").place(x=315, y=217)
    Label(edit_account_window, text='Current Password: ', bg = self.bgc, fg = "grey", font = "Arial").place(x=315, y=237)         
    Label(edit_account_window, text='New Username: ', bg = self.bgc, fg = "grey", font = "Arial").place(x=315, y=257)
    Label(edit_account_window, text='New Password: ', bg = self.bgc, fg = "grey", font = "Arial").place(x=315, y=277)

    usernameC = StringVar()
    passwordC = StringVar()

    usernameE = StringVar()
    passwordE = StringVar()

    Entry(edit_account_window, textvariable = usernameC, bg = 'light grey').place(x=450, y=220)
    Entry(edit_account_window, show='*', textvariable = passwordC, bg = 'light grey').place(x=450, y=240)
    Entry(edit_account_window, textvariable = usernameE, bg = 'light grey').place(x=450, y=260)
    Entry(edit_account_window, show='*', textvariable = passwordE, bg = 'light grey').place(x=450, y=280)

    edit_account_window_back = Button(edit_account_window, text=('Back'), width = 6, height = 1, bg = 'light grey', fg = 'black', font = ('Arial', 10), command=lambda:self.display(main_window))
    edit_account_window_back.place(x=27, y=750)

    updateB = Button(edit_account_window, text='UPDATE INFO', width = 11, command=lambda:self.CheckUpdateUser(usernameC, passwordC, usernameE, passwordE, main_window)) # This makes the register button, which will go to the CheckRegister def.
    updateB.place(x=472, y=300)

在检查了usernameC和passwordC是否在loginUsers表中之后,我想用usernameE和passwordE更新usernameC和passwordC。

请帮助!!

0 个答案:

没有答案