I've decided to try and make a login window using tkinter that will connect to a mysql database if the proper username and password are entered. I can connect automatically if I have that info already in the script, but can't quite make it work if it's entered into entry fields. I was trying something like this:
That's the basis of what I want and I've tried making the login information in a dictionary form first, then having it pass through conn = MySQLdb.connect but that fails as well. How can I properly format this so that the entered username and password will be used and not be hardcoded in the script?
Error 1045: Access denied for user '%s'@'localhost ' (using password: YES)
---that's the error message, obviously it's not replacing %s
Code:
from Tkinter import *
import sys
import MySQLdb
# Get the info entered in the root window entry fields
# and store them in variables f=(username), g=(password)
def SaveData():
f = a.get()
g = b.get()
# try to connect to mysql and use entered info (f,g)
try:
conn = MySQLdb.connect (host = "localhost",
user = "%s",
passwd = "%s",
db = "maindb") % f,g
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
cursor = conn.cursor ()
root = Tk()
ent_frame = Frame(root)
Label(ent_frame, text="USERNAME:").pack(side=LEFT)
a = Entry(ent_frame, width=15)
a.pack(side=LEFT)
Label(ent_frame, text="PASSWORD:").pack(side=LEFT)
b = Entry(ent_frame, width=15)
b.pack(side=LEFT)
ent_frame.pack()
Button(root, text="Login",command=SaveData).pack(side=BOTTOM)
mainloop()
Error 1045: Access denied for user '%s'@'localhost ' (using password: YES)
---that's the error message, obviously it's not replacing %s
Comment