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

Need Help Optomizing Code

P: n/a
Hi I am fairly new to python

and was wondering if anyone out there could help me optomize this code.

It is a simple Tkinter program that connects to a database and deletes
records

Some of this code was borrowed from a book learning python

Anyhelp would be cool thank you in advance

Andrew

"""""Below is the code"""""

import MySQLdb
from Tkinter import *

class FormEditor:

def __init__(self):
self.row = 0
self.current = None

self.root = root = Tk()
root.minsize(300, 200)
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
root.columnconfigure(1, weight=2)
Label(root, text='Form Editor App', font='bold').grid(columnspan=2)
self.row = self.row + 1
self.listbox = Listbox(root, selectmode=SINGLE)
self.listbox.grid(columnspan=2, sticky=E+W+N+S)
self.listbox.bind('<ButtonRelease>')
self.row = self.row + 1

self.add_button(self.root, self.row, 0, 'Delete Entry',
self.delentry)
self.add_button(self.root, self.row, 1, 'Reload', self.load_data)

self.load_data()
def add_button(self, root, row, column, text, command):
button = Button(root, text=text, command=command)
button.grid(row=row, column=column, sticky=E+W, padx=5, pady=5)

def load_data(self):
self.db = MySQLdb.connect("localhost", "", "", "guestbook")
self.c = self.db.cursor()
self.c.execute("select * from guests;")
self.results = self.c.fetchall()
self.listbox.delete(0, END)
for item in self.results:
self.listbox.insert(END, item)

def delentry(self):
self.db = MySQLdb.connect("localhost", "", "", "guestbook")
self.c = self.db.cursor()
self.c.execute("DELETE FROM guests WHERE id LIMIT 1;")
self.results = self.c.fetchall()
print "Please press reload to see your results"
app = FormEditor()
app.mainloop()
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Andrew:
and was wondering if anyone out there could help me optomize this code. [...] self.db = MySQLdb.connect("localhost", "", "", "guestbook")
self.c = self.db.cursor()
self.c.execute("DELETE FROM guests WHERE id LIMIT 1;") ^^^^^^^^^^^^^^^^
What does that mean?
self.results = self.c.fetchall()


In general, it's unwise to reconnect to a database server for every single
data manipulation. Keep the connection open, and perhaps even the cursor.
But I'm not sure if and how this works precisely with MySQL, I prefer
PostgreSQL myself.

Also, you seem to be forgetting the commit and disconnect.

--
René Pijlman
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.