473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Need Help Optomizing Code

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
1 1536
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: James Hong | last post by:
Help please, I try to sending an email from my html page using the java applet. but it give error on most of the PC only very few work, what is the error i make the java applet show as below ...
21
by: Chris Reedy | last post by:
For everyone - Apologies for the length of this message. If you don't want to look at the long example, you can skip to the end of the message. And for the Python gurus among you, if you can...
10
by: Jeff Wagner | last post by:
I am in the process of learning Python (obsessively so). I've been through a few tutorials and read a Python book that was lent to me. I am now trying to put what I've learned to use by rewriting...
10
by: Beach Potato | last post by:
Dear Y'all: I'm about to start porting a big old project written in anscient version of Delphi to something more stable, robust, supportable and maybe even portable. Since I haven't seriously...
48
by: Chad Z. Hower aka Kudzu | last post by:
A few of you may recognize me from the recent posts I have made about Indy <http://www.indyproject.org/indy.html> Those of you coming to .net from the Delphi world know truly how unique and...
4
by: Phil | last post by:
k, here is my issue.. I have BLOB data in SQL that needs to be grabbed and made into a TIF file and placed on the client (could be in temp internet dir). The reason we need it in TIF format is...
4
by: Quas.co.ua | last post by:
Hello all. I need your help. I need C compler to make demo of some technologie. This C compiler I need to write program which after run will be located in one segment of memory and it...
9
by: MrHelpMe | last post by:
Hello again experts, I have successfully pulled data from an LDAP server and now what I want to do is drop the data into a database table. The following is my code that will insert the data but...
20
by: mike | last post by:
I help manage a large web site, one that has over 600 html pages... It's a reference site for ham radio folks and as an example, one page indexes over 1.8 gb of on-line PDF documents. The site...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.