Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from tkinter.ttk import *
- import mysql.connector
- class sqlConsole(Frame):
- def __init__(self):
- self.root = Tk()
- self.width = "640"
- self.height = "480"
- self.cnx = None
- self.cursor = None
- self.grdResult = None
- self.vtxtCommand = StringVar()
- self.vtxtServer = StringVar()
- self.vtxtUser = StringVar()
- self.vtxtPwd = StringVar()
- Frame.__init__(self, self.root)
- self.createWidgets()
- self.layoutWidgets()
- self.vtxtServer.set("127.0.0.1")
- self.vtxtUser.set("root")
- self.vtxtPwd.set("")
- #self.bttConnect.invoke()
- self.txtCommand.focus()
- self.root.mainloop()
- def createWidgets(self):
- self.frmFrame = Frame(self.root, width=self.width, height=self.height)
- self.frmTop = Frame(self.frmFrame, width=self.width) #, height="100")
- self.frmBottom = Frame(self.frmFrame, width=self.width) #, height="380")
- self.lblLabel1 = Label(self.frmTop, text="Server:")
- self.lblLabel2 = Label(self.frmTop, text="User:")
- self.lblLabel3 = Label(self.frmTop, text="Password:")
- self.lblLabel4 = Label(self.frmTop, text="QUERY:")
- self.lblLabel5 = Label(self.frmTop, text="[ DISCONNECTED ]")
- self.lblKey = Label(self.frmTop, text="")
- self.txtServer = Entry(self.frmTop, textvariable=self.vtxtServer)
- self.txtUser = Entry(self.frmTop, textvariable=self.vtxtUser)
- self.txtPwd = Entry(self.frmTop, show="*", width=15, textvariable=self.vtxtPwd)
- self.txtCommand = Entry(self.frmTop, textvariable=self.vtxtCommand)
- self.grdResult = Treeview(self.frmBottom)
- self.lblStatus = Label(self.frmBottom, text="", wraplength=self.width)
- cmdbttExec = lambda: self.executeSQL(self.vtxtCommand.get(), self.grdResult)
- cmdbttConnect = lambda: self.connectSQL(self.txtServer.get(), \
- self.txtUser.get(), self.txtPwd.get())
- self.bttExec = Button(self.frmTop, text="Execute", width=10, command=cmdbttExec)
- self.bttConnect = Button(self.frmTop, text="Connect", width=10, command=cmdbttConnect)
- self.grdResult.column("#0", width=self.width)
- self.txtCommand.bind("<KeyPress>", self.keyPress)
- self.frmFrame.grid_propagate(0)
- def layoutWidgets(self):
- self.rowconfigure(0, weight=1)
- self.columnconfigure(0, weight=1)
- self.frmFrame.rowconfigure(0, weight=1)
- self.frmFrame.rowconfigure(1, weight=1)
- self.frmFrame.columnconfigure(0, weight=1, minsize=self.width)
- self.frmBottom.columnconfigure(0, weight=1, minsize=self.width)
- self.frmBottom.rowconfigure(0, weight=1, minsize=320)
- self.lblLabel1.grid(row=0, column=0, padx=5, pady=5, sticky="w")
- self.txtServer.grid(row=0, column=1, padx=5, pady=5, sticky="w")
- self.lblLabel5.grid(row=0, column=3, padx=5, pady=5, sticky="w")
- self.lblKey.grid(row=0, column=4, padx=5, pady=5, sticky="w")
- self.lblLabel2.grid(row=1, column=0, padx=5, pady=5, sticky="w")
- self.txtUser.grid(row=1, column=1, padx=5, pady=5, sticky="w")
- self.lblLabel3.grid(row=1, column=2, padx=5, pady=5, sticky="w")
- self.txtPwd.grid(row=1, column=3, padx=5, pady=5, sticky="w")
- self.bttConnect.grid(row=1, column=4, padx=5, pady=5, sticky="w")
- self.lblLabel4.grid(row=2, column=0, padx=5, pady=5, sticky="w")
- self.txtCommand.grid(row=2, column=1, columnspan=3, padx=5, pady=5, \
- sticky="nsew")
- self.bttExec.grid(row=2, column=4, padx=5, pady=5, sticky="new")
- self.grdResult.grid(row=0, column=0, columnspan=5, padx=5, pady=5, \
- sticky="nsew")
- self.lblStatus.grid(row=1, column=0, columnspan=5, rowspan=2, padx=5,\
- pady=5, sticky="ews")
- self.frmTop.grid(column=0, row=0, padx=5, pady=5, sticky="new")
- self.frmBottom.grid(column=0, row=1, padx=5, pady=5, sticky="nsew")
- self.frmFrame.grid(sticky="nsew")
- def keyPress(self, event):
- key = event.keysym
- self.lblKey.configure(text=key)
- if key == "Return":
- self.bttExec.invoke()
- elif key == "Escape":
- if self.txtCommand.get() == "":
- self.bttConnect.invoke()
- self.quit()
- else:
- self.vtxtCommand.set("")
- def executeSQL(self, sqlstr, grdResult):
- if sqlstr is not None:
- self.txtCommand.clipboard_clear()
- self.txtCommand.clipboard_append(sqlstr)
- print("QUERY: ",sqlstr,"\n")
- if self.cursor is not None:
- try:
- self.cursor.execute(sqlstr)
- if self.cursor.description is not None:
- self.resultset = self.cursor.fetchall()
- else:
- print("DES: ",self.cursor.description,"\n")
- if self.cursor.description is not None:
- columns = [x[0] for x in self.cursor.description]
- else:
- columns = []
- self.grdResult.delete(*self.grdResult.get_children(""))
- self.grdResult.configure(columns=columns)
- self.grdResult.column("#0", width=50)
- if len(columns) > 0:
- for row in columns:
- self.grdResult.column(row, width=200)
- self.grdResult.heading(row, text=row)
- x=0
- for row in self.resultset:
- x+=1
- self.grdResult.insert("" , "end", text=x, values=row)
- self.lblStatus.configure(text="Rows = " + str(x))
- else:
- self.lblStatus.configure(text="Executed.")
- except mysql.connector.Error as e:
- print("Error: ", e)
- self.lblStatus.configure(text=e)
- finally:
- self.vtxtCommand.set("")
- self.txtCommand.focus()
- def connectSQL(self, vServer, vUser, vPass):
- print(self.cnx)
- if self.cnx == None:
- try:
- self.cnx = mysql.connector.connect(user=vUser, password=vPass,\
- host=vServer)
- self.cursor = self.cnx.cursor()
- self.lblLabel5.configure(text="[ CONNECT ]")
- self.bttConnect.configure(text="Disconnect")
- except mysql.connector.Error as e:
- print(e)
- else:
- self.cnx.close()
- self.lblLabel5.configure(text="[ DISCONNECTED ]")
- self.bttConnect.configure(text="Connect")
- self.cnx = None
- self.cursor = None
- if __name__ == "__main__":
- app = sqlConsole()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement