help with tkinter/sql global error.

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

    help with tkinter/sql global error.

    I've hit another snag with tkinter and mysql. After the last post, I'm able to now have a user login that when username and password are entered, a successful mysql connection can be made. The problem now is that "cursor isn't global" and I'm not sure how to make it that way, or how to correct this code. So after logging in and clicking the "Next" button from the root window, I get error messages.


    Code:
    from Tkinter import *
    import sys
    import MySQLdb
    
     
    
    # Toplevel window that opens when "NEXT" button is clicked in "NextWindow" widget
    def NewWindow():
        win = Toplevel()
    
        # entry boxes put in a frame, text stored in variables k, l
        ent_frame = Frame(win)
        Label(ent_frame, text="ITEM:").pack(side=LEFT)
        k = Entry(ent_frame, width=15)
        k.pack(side=LEFT)
        Label(ent_frame, text="BRAND:").pack(side=LEFT)
        l = Entry(ent_frame, width=15)
        l.pack(side=LEFT)
        ent_frame.pack()
    
        # using get method for entry boxes above
        def SaveIt():
            m = k.get()
            n = l.get()
            # executing a sql command and inserting m,n
            cursor.execute ("""INSERT INTO info (item, brand) VALUES("%s", "%s")"""% (m,n))
            print "Number of rows inserted: %d" % cursor.rowcount
            conn.close()
           
        Button(win, text="Save",command=SaveIt).pack(side=BOTTOM)
        Button(win, text="Next", command=win.destroy).pack(side=BOTTOM)
    
    
    # Toplevel window that opens when "Next" is clicked from root window.
    def NextWindow():
        win = Toplevel()
    
        # entry boxes put in a frame, text stored in variables c,d
        ent_frame = Frame(win)
        Label(ent_frame, text="CITY:").pack(side=LEFT)
        c = Entry(ent_frame, width=15)
        c.pack(side=LEFT)
        Label(ent_frame, text="STATE:").pack(side=LEFT)
        d = Entry(ent_frame, width=15)
        d.pack(side=LEFT)
        ent_frame.pack()
    
        # using get method for entry boxes
        def SaveThis():
            h = c.get()
            j = d.get()
            # executing a sql command and inserting h,j
            cursor.execute ("""INSERT INTO info (city, state) VALUES("%s", "%s")"""% (h,j))
            print "Number of rows inserted: %d" % cursor.rowcount
            conn.close()
             
        # Click 'save' run 'SaveThis' and executes sql command. 'Next' opens toplevel
        Button(win, text="Save",command=SaveThis).pack(side=BOTTOM)
        Button(win, text="Next", command=NewWindow).pack(side=BOTTOM)
        
    
    
    # Takes text entered in root window of user & pass and enters them in sql connect
    def SaveData():
        f = a.get()
        g = b.get()
        try:
          conn = MySQLdb.connect (host = "localhost",
                                  user = "%s" % (f),
                                  passwd = "%s" % (g),
                                  db = "maindb") 
        # error if wrong username or password 
        except MySQLdb.Error, e:
          print "Error %d: %s" % (e.args[0], e.args[1])
          sys.exit (1)
    
          cursor = conn.cursor () 
         
    root = Tk()
     
    # frame containing 2 entries which will be stored in variables a,b
    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()
     
    # Clicking 'login' runs 'SaveData' and passes info into sql and connects with
    # username and password.
    Button(root, text="Login",command=SaveData).pack(side=BOTTOM)
    # opens a toplevel window with more entry fields
    Button(root, text="Next", command=NextWindow).pack(side=BOTTOM)
     
    mainloop()
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Could you post the complete error message including traceback? Could it be as simple as:
    Code:
    def SaveData():
        global cursor

    Comment

    • Thekid
      New Member
      • Feb 2007
      • 145

      #3
      That solved it, thanks! I was trying to set 'global' in "def SaveIt" and elsewhere because of the error. This was the basis of the error:
      In SaveIt line 22 NameError: global name 'cursor' is not defined

      Comment

      Working...