Need Help Optomizing Code

R

Rene Pijlman

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.
 
A

Andrew

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()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top