Interfacing With Mysql Our Database of Python Scripts Widgets in The Gui
Interfacing With Mysql Our Database of Python Scripts Widgets in The Gui
Interfacing With Mysql Our Database of Python Scripts Widgets in The Gui
Interfacing with MySQL our Database of Python Scripts Widgets in the GUI
7 March 2008 7 March 2008 7 March 2008
viewing it with a GUI
A GUI to browse a MySQL table A GUI to browse a
MySQL table
A GUI to browse a
MySQL table
A GUI to browse a
MySQL table
our database with Python scripts our database with Python
scripts We create one table scripts in the database
our database with Python
scripts
our database with Python
scripts
A GUI to browse a Code in __init__: A GUI to browse a Button defined in __init__: A GUI to browse a
MySQL table MySQL table MySQL table
our database with Python our database with Python our database with Python
scripts scripts scripts
connecting to database, self.message = StringVar() connecting to database, self.bc = Button(wdw, text=’connect’, \ connecting to database,
setting sort order setting sort order setting sort order
retrieving and displaying self.message.set("welcome to our database") retrieving and displaying command = self.connect) retrieving and displaying
records records records
self.messageLabel = Label(wdw, \ self.bc.grid(row=1,column=0)
Normalization Normalization Normalization
splitting a table in two
textvariable = self.message) splitting a table in two
self.cursor = 0 splitting a table in two
moving data into the tables
self.messageLabel.grid(row=0,column=0,columnspan=4) moving data into the tables moving data into the tables
We can sort on type, date, or file. A GUI to browse a A GUI to browse a def query(self): A GUI to browse a
MySQL table MySQL table MySQL table
"""
Code for RadioButton in __init__:
our database with Python
scripts User has choice between ascending or descending order. our database with Python
scripts
our database with Python
scripts
connecting to database, connecting to database, Returns the query to select all records, connecting to database,
setting sort order setting sort order setting sort order
retrieving and displaying Code for RadioButton in __init__: retrieving and displaying taking input from the radio buttons. retrieving and displaying
records records records
self.RadioSort = IntVar() """
Normalization Normalization Normalization
self.st = Radiobutton(wdw, text=’type’, \ splitting a table in two self.RadioOrder = IntVar() splitting a table in two q = ’select * from scripts’ splitting a table in two
moving data into the tables moving data into the tables moving data into the tables
variable = self.RadioSort, value = 1) self.asc = Radiobutton(wdw, text=’ascending order’, \ ord = ’’
Retrieving Specific Retrieving Specific Retrieving Specific
self.st.grid(row=4,column=1) Records variable = self.RadioOrder, value = 1) Records if self.RadioOrder.get() == 1: Records
self.sd = Radiobutton(wdw, text=’date’, \ use the key to access a
record self.asc.grid(row=5,column=0,columnspan=2) use the key to access a
record
ord = ’asc’ use the key to access a
record
if self.cursor == 0: Normalization 1. for type and number, e.g.: L-23 Normalization Normalization
splitting a table in two splitting a table in two
self.Lt.delete(0,END) splitting a table in two
self.message.set("please connect first") moving data into the tables self.Lt = Listbox(wdw,width=4,height=10) moving data into the tables
self.Ld.delete(0,END) moving data into the tables
Because our listboxes are only 10 in height, A GUI to browse a A GUI to browse a A GUI to browse a
MySQL table MySQL table MySQL table
we cannot display everything all at once. our database with Python our database with Python our database with Python
scripts def next10(self): scripts def prev10(self): scripts
command = self.next10) Retrieving Specific self.message.set("no records to show") Retrieving Specific self.message.set("no records to show") Retrieving Specific
Records Records Records
self.bn.grid(row=1,column=2) use the key to access a
else: use the key to access a
else: use the key to access a
record record record
self.clear() self.clear()
2. to show the previous 10 records: inserting a new record in a
table
inserting a new record in a
table
inserting a new record in a
table
for i in range(self.pos,self.pos+10): self.pos = self.pos - 20
self.bp = Button(wdw, text=’previous 10’, \ if i >= len(self.records): break if self.pos < 0: self.pos = 0
command = self.prev10) self.insert(self.records[i]) for i in range(self.pos,self.pos+10):
self.bp.grid(row=1,column=3) self.pos = self.pos + 10 if i >= len(self.records): break
if self.pos >= len(self.records): self.insert(self.records[i])
Storing the current position in the retrieved records: self.pos = len(self.records) - 1 self.pos = self.pos + 10
self.pos = 0
Two drawbacks:
records records
def CreateTables(c): records
and not so convenient to use as key Retrieving Specific c = db.cursor() Retrieving Specific
the tables typedate and filedata.
Retrieving Specific
Records Records Records
2. as most lectures featured more than one script, use the key to access a q = ’select * from scripts order by d’ use the key to access a use the key to access a
record record The input parameter c is the cursor. record
there is a lot of redundant data in the table inserting a new record in a lc = c.execute(q) inserting a new record in a inserting a new record in a
table table """ table
The table typedate stores type and date, retrieving and displaying The table filedata stores file names, retrieving and displaying in typedate and in filedata. retrieving and displaying
records records records
e.g.: L-4 and 2008-01-23, repesented by represented by three fields: id, name, """
Normalization Normalization Normalization
splitting a table in two splitting a table in two L = []; T = []; F = []; cnt = -1 splitting a table in two
four fields: id, type, number, and date. moving data into the tables
and id of the corresponding entry in moving data into the tables moving data into the tables
for i in range(0,len(R)):
The input parameter c is the cursor. Retrieving Specific the typedate table. c is the cursor. Retrieving Specific
d = R[i] Retrieving Specific
Records Records Records
""" use the key to access a """ use the key to access a
p = (d[0],d[1],d[2]) use the key to access a
record record record
Given the cursor and a list of values for Normalization Given the cursor and a list of values for Normalization As a final check, selects file name Normalization
the table typedate, all records are added. splitting a table in two
moving data into the tables the table filedata, all records are added.
splitting a table in two
moving data into the tables
and corresonding date from the newly splitting a table in two
moving data into the tables
""" Retrieving Specific """ Retrieving Specific created tables. c is the cursor. Retrieving Specific
Records Records Records
for i in range(0,len(T)): """
use the key to access a
record for i in range(0,len(F)): use the key to access a
record
use the key to access a
record
p = T[i] inserting a new record in a
n = F[i] inserting a new record in a q = ’select f,d from filedata, typedate’ \ inserting a new record in a
table table table
q = ’insert into typedate values ’ + \ q = ’insert into filedata values ’ + \ + ’ where filedata.t = typedate.i’
’(\"%s\", \"%s\", \"%s\", \"%s\") ’ \ ’(\"%s\", \"%s\", \"%s\") ’ \ c.execute(q)
% (str(p[0]), str(p[1]), str(p[2]), \ % (str(n[0]), n[1], str(n[2])) R = c.fetchall()
str(p[3])) c.execute(q) print R
c.execute(q) print ’#records :’, len(R)
MCS 275 L-23 MCS 275 L-23 MCS 275 L-23
Using Keys The Widgets in the GUI Query for Specific Record
7 March 2008 7 March 2008 7 March 2008
After the normalization, every script has a unique key. Normalization Normalization
""" Normalization
This key is in the i field of the table filedata. splitting a table in two splitting a table in two Returns the query for all the information splitting a table in two
moving data into the tables moving data into the tables moving data into the tables
of the script with the given key.
Retrieving Specific Retrieving Specific Retrieving Specific
Accessing the data via the key allows us to Records Records """ Records
use the key to access a use the key to access a use the key to access a
def CountFiles(self):
records records
Asking to confirm the data: records
n = self.cursor.fetchone()
return int(n[0])
MCS 275 L-23 MCS 275 L-23 MCS 275 L-23
Inserting a new File Inserting Data Inserting a Record
7 March 2008 7 March 2008 7 March 2008
Retrieving Specific
f = self.nm.get() Retrieving Specific
t = L[0]; n = L[1] Retrieving Specific
Records r = (t,d,f) Records d = data[1]; f = data[2] Records
use the key to access a use the key to access a use the key to access a
record
if not self.startinsert: record nf = self.CountFiles() record
inserting a new record in a inserting a new record in a inserting a new record in a
table
m = ’inserting %s,’ % str(r) table nt = self.CountTypes() table
def InsertFileValues(self,v):
moving data into the tables 1. Create the equivalent to guidb1.py, our first GUI, moving data into the tables
"""
Retrieving Specific
Records
writing a CGI script. Retrieving Specific
Records