在python中从MySql数据库提取数据时出错

8hhllhi2  于 2023-02-03  发布在  Mysql
关注(0)|答案(1)|浏览(134)

我尝试使用Python中的WHERE子句从Mysql数据库中获取密码,并将密码存储在变量“DbPassword”中,但当我打印“DbPassword”时,它打印“None”,有人能解释一下为什么会发生这种情况,以及我如何解决这个问题吗?谢谢I have attached a ScreenShot of my IDE where the entire function and Output can be seen

def searchdb(self):        
    User = (self.Username.get())
    pas = (self.Password.get())
   
    myconn = mysql.connector.connect(host = "localhost", user = "root", passwd = "qwerty321", database = "admin")
    
    if len(User) == 0 and len(pas) == 0 :
       tkinter.messagebox.showinfo("Pharmacy Management System","Please fill in the Login Details")
    elif len(User) == 0 and len(pas) != 0 :
        tkinter.messagebox.showinfo("Pharmacy Management System","Please Enter a Username")
    elif len(User) != 0 and len(pas) == 0:
         tkinter.messagebox.showinfo("Pharmacy Management System","Please enter a Password")
    else:
        try:
            #seacrch data
            #print("%s\n%s"%(User,pas) )
            
            sql = "SELECT password FROM users WHERE user_name = '%s'"
            cur = myconn.cursor()
            cur.execute(sql,(User))
           
            DbPassword = cur.fetchone()
            
            print("%s"%(DbPassword))
            
            if (pas == str(DbPassword)) :
                self.btnRegistration.config(state=NORMAL)
                self.btnHospital.config(state=NORMAL)
            else:
                tkinter.messagebox.askokcancel("Pharmacy Management System","You have entered an invalid login details")
                self.btnRegistration.config(state=DISABLED)
                self.btnHospital.config(state=DISABLED)
                self.Username.set("")
                self.Password.set("")
                self.txtUsername.focus()
        except Error as e:
               # tkinter.messagebox.showinfo("Pharmacy Management System","NO RECORD FOUND !")
                myconn.rollback()
                Window1.Reset(self)
    myconn.close()
kyxcudwk

kyxcudwk1#

最后得到了答案做了一些修改修改:sql = "SELECT password FROM users WHERE user_name = '%s'" cur.execute(sql,(User))至:* * sql = "SELECT password FROM users WHERE email= '"+ self.Username.get() +"'" cur.execute(sql)**并且它正在按预期工作。

相关问题