help making a tk/msql user login

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Thekid
    New Member
    • Feb 2007
    • 145

    help making a tk/msql user login

    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:

    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()
    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
    Last edited by Thekid; Nov 11 '10, 10:21 PM. Reason: Added error message.
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    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:
    Code:
    >>> a = 123
    >>> b = 456
    >>> "Variables a and b are set to %s and %s respectively." % (a, b)
    'Variables a and b are set to 123 and 456 respectively.'
    >>>
    So your code should be:
    Code:
          conn = MySQLdb.connect (host = "localhost",
                                  user = "%s" % (f),
                                  passwd = "%s" % (g),
                                  db = "maindb")
    Last edited by bvdet; Nov 12 '10, 02:14 AM.

    Comment

    • Thekid
      New Member
      • Feb 2007
      • 145

      #3
      Oh, I see. So close :) Thanks.

      Comment

      Working...