How can I create a python (Tkinter) form that will search a mysql table for a specific row, then allow me to UPDATE that row.

Posted on 2009-04-21
Last Modified: 2012-05-06
How can I create a python/Tkinter form that allows me to search for a particular row in a MySQL table, then UPDATE that row as needed.
I got some help last week from cxr (expert) in putting together a form (see below) that we use to INSERT new records. We've been able to modify what cxr did, such that it now works for other, larger tables.
If someone could assist me with some modifications to this code, we'll be able to take it from there.

from Tkinter import *

import MySQLdb


## Connect to DB

conn = MySQLdb.connect(host='localhost', user='root', db='mind0')

##Create a cursor

curs = conn.cursor() 

## *****************************************************

# Root level form

class MyApp:

  def __init__(self):

    window = Tk()

    window.title('Data Entry Form')   

    frame = Frame(window)


## BEGIN Fields ***********************************************     

    self.fields = {}


    l = Label(frame, text="vendID:")

    l.grid(row=0, column=0)    

    self.fields['vendID'] = Entry(frame)

    self.fields['vendID'].grid(row=0, column=1)    


    l = Label(frame, text="vendName:")

    l.grid(row=2, column=0)   

    self.fields['vendName'] = Entry(frame)

    self.fields['vendName'].grid(row=2, column=1)

## END Fields ************************************************    


## BEGIN Buttons ########         

    submitbtn = Button(frame, text="Find", command=self.do_insert)

    submitbtn.grid(row=11, column=0)


    clearbtn = Button(frame, text="Clear", command=self.do_clear)

    clearbtn.grid(row=11, column=1)

## END Buttons ##########    



## Insert and Clear functions 

  def do_clear(self):  




  def do_insert(self):

  	global curs

	sql = "insert into vendor (vendID,vendName) values ('%s','%s');"%( 




	print sql


if __name__=="__main__":


Open in new window

Question by:lessthan2
    LVL 39

    Accepted Solution

    Make an update-button, attach it to this method:
      def do_update(self):
            global curs
            sql = "update vendor set vendName='%s' where vendID='%s')"%( 
            print sql

    Open in new window


    Author Comment

    cxr, thanks for the solution (I was hoping you'd take my question). With a couple minor edits, it works fine for doing INSERTs, and UPDATEs.
    Next, I'm going to try and build some searching functionality into these forms. If I end up needing some guidance, I'll post another question. Keep an eye out for me.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
    Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
    Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now