• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 551
  • Last Modified:

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.

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

1 Solution
Roger BaklundCommented:
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

lessthan2Author Commented:
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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now