By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,965 Members | 1,450 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,965 IT Pros & Developers. It's quick & easy.

help making a tk/msql user login

Thekid
100+
P: 145
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:

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import sys
  3. import MySQLdb
  4.  
  5.  
  6. # Get the info entered in the root window entry fields
  7. # and store them in variables f=(username), g=(password)
  8. def SaveData():
  9.     f = a.get()
  10.     g = b.get()
  11.  
  12.     # try to connect to mysql and use entered info (f,g)
  13.     try:
  14.       conn = MySQLdb.connect (host = "localhost",
  15.                               user = "%s",
  16.                               passwd = "%s",
  17.                               db = "maindb") % f,g
  18.     except MySQLdb.Error, e:
  19.       print "Error %d: %s" % (e.args[0], e.args[1])
  20.       sys.exit (1)
  21.  
  22.     cursor = conn.cursor () 
  23.  
  24. root = Tk()
  25.  
  26. ent_frame = Frame(root)
  27. Label(ent_frame, text="USERNAME:").pack(side=LEFT)
  28. a = Entry(ent_frame, width=15)
  29. a.pack(side=LEFT)
  30. Label(ent_frame, text="PASSWORD:").pack(side=LEFT)
  31. b = Entry(ent_frame, width=15)
  32. b.pack(side=LEFT)
  33. ent_frame.pack()
  34.  
  35. Button(root, text="Login",command=SaveData).pack(side=BOTTOM)
  36.  
  37. mainloop()
  38.  
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
Nov 11 '10 #1

✓ answered by bvdet

You are close. Your string formatting was not done properly. The modulo operator produces a formatted string given a format string and a collection of objects in a tuple or dictionary. Example:
Expand|Select|Wrap|Line Numbers
  1. >>> a = 123
  2. >>> b = 456
  3. >>> "Variables a and b are set to %s and %s respectively." % (a, b)
  4. 'Variables a and b are set to 123 and 456 respectively.'
  5. >>> 
So your code should be:
Expand|Select|Wrap|Line Numbers
  1.       conn = MySQLdb.connect (host = "localhost",
  2.                               user = "%s" % (f),
  3.                               passwd = "%s" % (g),
  4.                               db = "maindb")

Share this Question
Share on Google+
2 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
You are close. Your string formatting was not done properly. The modulo operator produces a formatted string given a format string and a collection of objects in a tuple or dictionary. Example:
Expand|Select|Wrap|Line Numbers
  1. >>> a = 123
  2. >>> b = 456
  3. >>> "Variables a and b are set to %s and %s respectively." % (a, b)
  4. 'Variables a and b are set to 123 and 456 respectively.'
  5. >>> 
So your code should be:
Expand|Select|Wrap|Line Numbers
  1.       conn = MySQLdb.connect (host = "localhost",
  2.                               user = "%s" % (f),
  3.                               passwd = "%s" % (g),
  4.                               db = "maindb")
Nov 11 '10 #2

Thekid
100+
P: 145
Oh, I see. So close :) Thanks.
Nov 12 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.