Thursday, 25 December 2014

select and describe table in MySQL by using wxpython

#!/usr/bin/python

import wx
import MySQLdb

###############################################
class func:

   #------------------------------------
   def __init__(self,opt):
       """Constractor"""
       self.opt = opt

###############################################
class dbOpt():
   #------------------------------------
   def __init__(self,host,user,passwd,dbname,tbname):
       self.host = host
       self.user = user
       self.passwd = passwd
       self.dbname = dbname
       self.tbname = tbname

   #------------------------------------
   def dbSelect(self):
       db = MySQLdb.connect(host=self.host, user=self.user, passwd=self.passwd, db=self.dbname)
       selectSql = "SELECT * FROM " + self.tbname
       cursor = db.cursor()
       cursor.execute(selectSql)
       db.commit()
       data = cursor.fetchall()
       cursor.close()
       db.close()
       data = "{}".format(data)
       return data

   #------------------------------------
   def dbDescribe(self):
       db = MySQLdb.connect(host="localhost", user="root", passwd="1988628",db=self.dbname)
       selectSql = "describe " + self.tbname
       cursor = db.cursor()
       cursor.execute(selectSql)
       db.commit()
       data = cursor.fetchall()
       cursor.close()
       db.close()
       data = "{}".format(data)
       return data

###############################################
class myFrame(wx.Frame):
   #-------------------------------------
   def __init__(self):
       wx.Frame.__init__(self, None, wx.ID_ANY, "Colin's SQL")

       # Add a panel, so it looks the correct on all platfroms
       panel = wx.Panel(self, wx.ID_ANY)

       opts = [func("show tables"),
              func("describe")]
       sampleList = []
       self.cb = wx.ComboBox(panel, size=wx.DefaultSize,choices=sampleList)
       self.widgetMaker(self.cb, opts)
       self.dbhost = wx.TextCtrl(panel)
       self.dbuser = wx.TextCtrl(panel)
       self.dbpasswd = wx.TextCtrl(panel)
       self.dbfield = wx.TextCtrl(panel)
       self.tbfield = wx.TextCtrl(panel)
       self.contents = wx.TextCtrl(panel,style=wx.TE_MULTILINE|wx.HSCROLL)

       tsizer = wx.BoxSizer()
       tsizer.Add(self.dbhost, 1, wx.EXPAND)
       tsizer.Add(self.dbuser, 1, wx.EXPAND)
       tsizer.Add(self.dbpasswd, 1, wx.EXPAND)

       hsizer = wx.BoxSizer()
       hsizer.Add(self.dbfield, 1, wx.EXPAND)
       hsizer.Add(self.tbfield, 1, wx.EXPAND)
       hsizer.Add(self.cb, 0, wx.RIGHT, 5)

       vsizer = wx.BoxSizer(wx.VERTICAL)
       vsizer.Add(tsizer, 0, wx.EXPAND, 5)
       vsizer.Add(hsizer, 0, wx.EXPAND, 5)
       vsizer.Add(self.contents, 1, wx.EXPAND|wx.LEFT|wx.RIGHT|wx.BOTTOM)

       panel.SetSizer(vsizer)

   #-------------------------------------
   def widgetMaker(self, widget, objects):
       """"""
       for obj in objects:
           widget.Append(obj.opt, obj)
       widget.Bind(wx.EVT_COMBOBOX, self.onSelect)

   #-------------------------------------
   def onSelect(self, event):
       obj = self.cb.GetClientData(self.cb.GetSelection())
       if obj.opt == "show tables":
           stb=dbOpt(self.dbhost.GetValue(),self.dbuser.GetValue(),self.dbpasswd.GetValue(),self.dbfield.GetValue(),self.tbfield.GetValue())
           self.contents.SetValue(stb.dbSelect())
       elif obj.opt == "describe":
           stb=dbOpt(self.dbhost.GetValue(),self.dbuser.GetValue(),self.dbpasswd.GetValue(),self.dbfield.GetValue(),self.tbfield.GetValue())
           self.contents.SetValue(stb.dbDescribe())

# Run the Program
if __name__ == "__main__":
   app = wx.App(False)
   frame = myFrame()
   frame.Show()
app.MainLoop()