Advertisement
LordRip

SQL Command Interpreter

Jan 16th, 2016
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.89 KB | None | 0 0
  1. from tkinter import *
  2. from tkinter.ttk import *
  3. import mysql.connector
  4.  
  5.  
  6. class sqlConsole(Frame):
  7.     def __init__(self):
  8.         self.root = Tk()
  9.         self.width = "640"
  10.         self.height = "480"
  11.         self.cnx = None
  12.         self.cursor = None
  13.         self.grdResult = None
  14.         self.vtxtCommand = StringVar()
  15.         self.vtxtServer = StringVar()
  16.         self.vtxtUser = StringVar()
  17.         self.vtxtPwd = StringVar()
  18.        
  19.         Frame.__init__(self, self.root)
  20.         self.createWidgets()
  21.         self.layoutWidgets()
  22.  
  23.         self.vtxtServer.set("127.0.0.1")
  24.         self.vtxtUser.set("root")
  25.         self.vtxtPwd.set("")
  26.         #self.bttConnect.invoke()
  27.         self.txtCommand.focus()
  28.  
  29.         self.root.mainloop()
  30.  
  31.  
  32.     def createWidgets(self):
  33.         self.frmFrame = Frame(self.root, width=self.width, height=self.height)
  34.         self.frmTop = Frame(self.frmFrame, width=self.width) #, height="100")
  35.         self.frmBottom = Frame(self.frmFrame, width=self.width) #, height="380")
  36.  
  37.         self.lblLabel1 = Label(self.frmTop, text="Server:")
  38.         self.lblLabel2 = Label(self.frmTop, text="User:")
  39.         self.lblLabel3 = Label(self.frmTop, text="Password:")
  40.         self.lblLabel4 = Label(self.frmTop, text="QUERY:")
  41.         self.lblLabel5 = Label(self.frmTop, text="[ DISCONNECTED ]")
  42.         self.lblKey = Label(self.frmTop, text="")
  43.  
  44.         self.txtServer = Entry(self.frmTop, textvariable=self.vtxtServer)
  45.         self.txtUser = Entry(self.frmTop, textvariable=self.vtxtUser)
  46.         self.txtPwd = Entry(self.frmTop, show="*", width=15, textvariable=self.vtxtPwd)
  47.         self.txtCommand = Entry(self.frmTop, textvariable=self.vtxtCommand)
  48.         self.grdResult = Treeview(self.frmBottom)
  49.         self.lblStatus = Label(self.frmBottom, text="", wraplength=self.width)
  50.  
  51.         cmdbttExec = lambda: self.executeSQL(self.vtxtCommand.get(), self.grdResult)
  52.         cmdbttConnect = lambda: self.connectSQL(self.txtServer.get(), \
  53.             self.txtUser.get(), self.txtPwd.get())
  54.         self.bttExec = Button(self.frmTop, text="Execute", width=10, command=cmdbttExec)
  55.         self.bttConnect = Button(self.frmTop, text="Connect", width=10, command=cmdbttConnect)
  56.  
  57.         self.grdResult.column("#0", width=self.width)
  58.         self.txtCommand.bind("<KeyPress>", self.keyPress)
  59.         self.frmFrame.grid_propagate(0)
  60.  
  61.  
  62.     def layoutWidgets(self):
  63.         self.rowconfigure(0, weight=1)
  64.         self.columnconfigure(0, weight=1)
  65.         self.frmFrame.rowconfigure(0, weight=1)
  66.         self.frmFrame.rowconfigure(1, weight=1)
  67.         self.frmFrame.columnconfigure(0, weight=1, minsize=self.width)
  68.         self.frmBottom.columnconfigure(0, weight=1, minsize=self.width)
  69.         self.frmBottom.rowconfigure(0, weight=1, minsize=320)
  70.  
  71.         self.lblLabel1.grid(row=0, column=0, padx=5, pady=5, sticky="w")
  72.         self.txtServer.grid(row=0, column=1, padx=5, pady=5, sticky="w")
  73.         self.lblLabel5.grid(row=0, column=3, padx=5, pady=5, sticky="w")
  74.         self.lblKey.grid(row=0, column=4, padx=5, pady=5, sticky="w")
  75.  
  76.         self.lblLabel2.grid(row=1, column=0, padx=5, pady=5, sticky="w")
  77.         self.txtUser.grid(row=1, column=1, padx=5, pady=5, sticky="w")
  78.  
  79.         self.lblLabel3.grid(row=1, column=2, padx=5, pady=5, sticky="w")
  80.         self.txtPwd.grid(row=1, column=3, padx=5, pady=5, sticky="w")
  81.         self.bttConnect.grid(row=1, column=4, padx=5, pady=5, sticky="w")
  82.  
  83.         self.lblLabel4.grid(row=2, column=0, padx=5, pady=5, sticky="w")
  84.         self.txtCommand.grid(row=2, column=1, columnspan=3, padx=5, pady=5, \
  85.             sticky="nsew")        
  86.         self.bttExec.grid(row=2, column=4, padx=5, pady=5, sticky="new")
  87.  
  88.         self.grdResult.grid(row=0, column=0, columnspan=5, padx=5, pady=5, \
  89.             sticky="nsew")        
  90.         self.lblStatus.grid(row=1, column=0, columnspan=5, rowspan=2, padx=5,\
  91.             pady=5, sticky="ews")
  92.         self.frmTop.grid(column=0, row=0, padx=5, pady=5, sticky="new")
  93.         self.frmBottom.grid(column=0, row=1, padx=5, pady=5, sticky="nsew")
  94.         self.frmFrame.grid(sticky="nsew")
  95.  
  96.  
  97.     def keyPress(self, event):
  98.         key = event.keysym
  99.         self.lblKey.configure(text=key)
  100.         if key == "Return":
  101.             self.bttExec.invoke()
  102.        
  103.         elif key == "Escape":
  104.             if self.txtCommand.get() == "":
  105.                 self.bttConnect.invoke()
  106.                 self.quit()
  107.             else:
  108.                 self.vtxtCommand.set("")
  109.  
  110.  
  111.     def executeSQL(self, sqlstr, grdResult):
  112.         if sqlstr is not None:
  113.             self.txtCommand.clipboard_clear()
  114.             self.txtCommand.clipboard_append(sqlstr)
  115.             print("QUERY: ",sqlstr,"\n")
  116.  
  117.         if self.cursor is not None:
  118.             try:
  119.                 self.cursor.execute(sqlstr)
  120.                 if self.cursor.description is not None:
  121.                     self.resultset = self.cursor.fetchall()
  122.                 else:
  123.                     print("DES: ",self.cursor.description,"\n")
  124.  
  125.                 if self.cursor.description is not None:
  126.                     columns = [x[0] for x in self.cursor.description]
  127.                 else:
  128.                     columns = []
  129.  
  130.                 self.grdResult.delete(*self.grdResult.get_children(""))
  131.                 self.grdResult.configure(columns=columns)
  132.                 self.grdResult.column("#0", width=50)
  133.  
  134.                 if len(columns) > 0:
  135.                     for row in columns:
  136.                         self.grdResult.column(row, width=200)
  137.                         self.grdResult.heading(row, text=row)
  138.                     x=0
  139.                     for row in self.resultset:
  140.                         x+=1
  141.                         self.grdResult.insert("" , "end", text=x, values=row)
  142.  
  143.                     self.lblStatus.configure(text="Rows = " + str(x))
  144.                 else:
  145.                     self.lblStatus.configure(text="Executed.")
  146.  
  147.             except mysql.connector.Error as e:
  148.                 print("Error: ", e)
  149.                 self.lblStatus.configure(text=e)
  150.             finally:
  151.                 self.vtxtCommand.set("")
  152.                 self.txtCommand.focus()
  153.  
  154.  
  155.     def connectSQL(self, vServer, vUser, vPass):
  156.         print(self.cnx)
  157.         if self.cnx == None:
  158.             try:
  159.                 self.cnx = mysql.connector.connect(user=vUser, password=vPass,\
  160.                     host=vServer)
  161.                 self.cursor = self.cnx.cursor()
  162.  
  163.                 self.lblLabel5.configure(text="[ CONNECT ]")
  164.                 self.bttConnect.configure(text="Disconnect")
  165.            
  166.             except mysql.connector.Error as e:
  167.                 print(e)
  168.         else:
  169.             self.cnx.close()
  170.             self.lblLabel5.configure(text="[ DISCONNECTED ]")
  171.             self.bttConnect.configure(text="Connect")
  172.             self.cnx = None
  173.             self.cursor = None
  174.  
  175.  
  176.  
  177. if __name__ == "__main__":
  178.     app = sqlConsole()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement