Error while fetching data from MySql Database in python
Question:
I am trying to fetch password from Mysql database using WHERE clause in Python and storing the password in a variable "DbPassword" but when I am printing the "DbPassword" it prints "None"
can anyone explain why this is happening and how can I solve this?
Thank you 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("%sn%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()
Answers:
Finally got the answer made some changes
modified:
sql = "SELECT password FROM users WHERE user_name = '%s'" cur.execute(sql,(User))
to :
sql = "SELECT password FROM users WHERE email= '"+ self.Username.get() +"'"
cur.execute(sql)
and it is working as expected.
I am trying to fetch password from Mysql database using WHERE clause in Python and storing the password in a variable "DbPassword" but when I am printing the "DbPassword" it prints "None"
can anyone explain why this is happening and how can I solve this?
Thank you 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("%sn%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()
Finally got the answer made some changes
modified:
sql = "SELECT password FROM users WHERE user_name = '%s'" cur.execute(sql,(User))
to :
sql = "SELECT password FROM users WHERE email= '"+ self.Username.get() +"'"
cur.execute(sql)
and it is working as expected.