Skip to content

Commit

Permalink
added functionality of connecting to a db
Browse files Browse the repository at this point in the history
  • Loading branch information
wrawler committed Jan 7, 2023
1 parent c4a8eea commit 9d9cb36
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 21 deletions.
9 changes: 3 additions & 6 deletions PyDbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import mysql.connector
from mysql.connector import errorcode


## Creating connection object
hostname = input("\n>>PLEASE ENTER YOUR HOSTNAME (By default: localhost):" ) #no actuall need to enter you username and passwd if you have default ones
username = input(">>PLEASE ENTER YOUR USERNAME(By default: root ):")
Expand Down Expand Up @@ -61,7 +60,7 @@ def database_create():
err_handle()

else:
print(">>Table created with no errors..")
print(">>Database created with no errors..")

err_handle()

Expand Down Expand Up @@ -117,7 +116,6 @@ def table_creator():
print("\nKindly first create the initiating column for table, you would be given choice to create table with as much columns you want...")
# first created a table with one column and added more columns showing that all the columns were made at time of table creations


a = input("NAME FOR COLUMN 1:")
t = input("DATATYPE OF COLUMN 1:")
size = input("SIZE OF COLUMN 1:")
Expand All @@ -130,8 +128,8 @@ def table_creator():
elif choice1 == "n":
cursor_obj.execute("CREATE TABLE {} ({} {}({}))".format(table_name,a,t,size))

x = int(input("PLEASE ENTER THE NUMBER OF COLUMNS YOU WANT IN TABLE:")) #determing the desired degree of table
i = 1
x = int(input("PLEASE ENTER THE NUMBER OF COLUMNS YOU WANT IN TABLE:")) #determining the desired degree of table
i = 1
while i < x: #loop to add columns to table
a1 = input("NAME FOR COLUMN {}:".format(i+1))
t1 = input("DATATYPE FOR COLUMN {}:".format(i+1))
Expand Down Expand Up @@ -190,7 +188,6 @@ def commit():

err_handle()


## To input values to a table
def inputer():
choice = input(">>DO YOU WANT TO SEE A LIST OF TABLES TO SELECT? y or no:")
Expand Down
126 changes: 111 additions & 15 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,73 @@
from tkinter import filedialog
import os
from PIL import Image, ImageDraw, ImageTk
import mysql.connector
from mysql.connector import errorcode

#_________Creating a function to connect to mysql database


def connect(a,b,c,d):
try:
mydb = mysql.connector.connect(
host = "{}".format(a),
user = "{}".format(b),
password = "{}".format(c),
database = "{}".format(d)
)

if mydb.is_connected():
print("Connected to Database")

except mysql.connector.DatabaseError as ez3:
if ez3.errno == 2005:
print("\nNo host present named",'"',a,'"',"\nKindly rerun the program")
exit()

except mysql.connector.ProgrammingError as ez:
if errorcode.ER_ACCESS_DENIED_ERROR == ez.errno:
print("\n>>Access Denied: Kindly recheck your connection parameters..")
quit()

except mysql.connector.InterfaceError as ez2:
if ez2.errno == 2003:
print("\n>>ERROR: Kindly recheck your connection parameters\n\t\tIf you think parameters are correct, check if mysql service is enabled...\n")
quit()

if mydb.is_connected():
print("Connected to Database")

#__________Creating a window to connect to mysql database_________

def connector_window():
top1 = Toplevel(root)
top1.transient(root)
top1.geometry("550x300")
top1.geometry("550x250")
top1.title("Connect")
top1.resizable(False,False)

e1 = Entry(top1,width=30,relief=RAISED,font=("Calibre",13)).grid(row=0,column=2,pady=10,ipadx=0)
e2 = Entry(top1,width=30,relief=RAISED,font=("Calibre",13)).grid(row=1,column=2,pady=10,ipadx=0)
e3 = Entry(top1,width=30,relief=RAISED,font=("Calibre",13),show="*").grid(row=2,column=2,pady=10,ipadx=0)
e4 = Entry(top1,width=30,relief=RAISED,font=("Calibre",13)).grid(row=3,column=2,pady=10,ipadx=0)
e1 = Entry(top1,width=30,relief=RAISED,font=("Calibre",13))
e2 = Entry(top1,width=30,relief=RAISED,font=("Calibre",13))
e3 = Entry(top1,width=30,relief=RAISED,font=("Calibre",13),show="*")
e4 = Entry(top1,width=30,relief=RAISED,font=("Calibre",13))

e1.place(x=200,y=20,anchor="nw")
e2.place(x=200,y=60,anchor="nw")
e3.place(x=200,y=100,anchor="nw")
e4.place(x=200,y=140,anchor="nw")

l1 = Label(top1,text="HOST:",font=("Arial",20)).grid(row=0,column=0,pady=10,ipadx=0)
l2 = Label(top1,text="USER",font=("Arial",20)).grid(row=1,column=0,pady=10,ipadx=0)
l3 = Label(top1,text="PASSWORD:",font=("Arial",20)).grid(row=2,column=0,pady=10,ipadx=0)
l4 = Label(top1,text="DATABASE:",font=("Arial",20)).grid(row=3,column=0,pady=10,ipadx=0)
l1 = Label(top1,text="HOST:",font=("Arial",20)).place(x=10,y=20,anchor="nw")
l2 = Label(top1,text="USER",font=("Arial",20)).place(x=10,y=60,anchor="nw")
l3 = Label(top1,text="PASSWORD:",font=("Arial",20)).place(x=10,y=100,anchor="nw")
l4 = Label(top1,text="DATABASE:",font=("Arial",20)).place(x=10,y=140,anchor="nw")

connect_button = Button(top1,text="Connect",font=("Arial",15),command=top1.destroy).grid(row=4,column=0,pady=20)
cancel_button = Button(top1,text="Cancel",font=("Arial",15),command=top1.destroy).grid(row=4,column=1,pady=20)
hostname = e1.get()
username = e2.get()
passwd = e3.get()
dbaname = e4.get()

connect_button = Button(top1,text="Connect",font=("Arial",15),command=lambda:[connect(hostname,username,passwd,dbaname),top1.destroy()]).place(x=150,y=200,anchor="nw")
cancel_button = Button(top1,text="Cancel",font=("Arial",15),command=top1.destroy).place(x=300,y=200,anchor="nw")

def quit():
top2 = Toplevel(root)
Expand All @@ -36,10 +83,55 @@ def quit():
top2.transient(root)
top2.resizable(False,False)


#___________Creating a window to open a file from desired directory________

def file_dialog_box():
root.filename = filedialog.askopenfilename(initialdir = "/",title="Select A File",filetypes = (("Database file","*.db"),("All Files","*.*")))

#root window

#_________Creating a new window similar to root window to create a new file_____________


def newfileframe():
top3 = Toplevel(root)
top3.geometry("1920x1080")
top3.title("PyDBMS")

toolbar_frame2 = Frame(top3,width=1920,height=60,bg="gray22").place(x=0,y=0,anchor="nw")
output_frame2 = Frame(top3,width=1440,height=1020,bg="gray16").place(x=480,y=60,anchor="nw")
foo_frame2 = Frame(top3,width=1920,height=30,bg="gray19").place(x=0,y=60,anchor="nw")
querry_frame2 = Frame(top3,width=480,height=1020,bg="gray20").place(x=0,y=0,anchor="nw")

conn_button2 = Button(top3,padx=2,text="CONNECT",font = ("Arial",12),command = connector_window,bg="gray27",fg="white",highlightthickness = 0,relief=RAISED).pack(anchor="nw")

top3.option_add('*tearOff', FALSE)
my_menu2 = Menu(toolbar_frame2,bg="gray30",fg="White",font=("Calibri",12),relief=RAISED)
top3.config(menu = my_menu2)

file_menu = Menu(my_menu2)
my_menu2.add_cascade(label = "FILE",menu = file_menu)
file_menu.add_command(label = "New",command=newfileframe)
file_menu.add_command(label = "Open File",command = file_dialog_box)
file_menu.add_command(label = "Save as")
file_menu.add_command(label = "Save")
file_menu.add_command(label = "Exit",command = quit)

database_menu = Menu(my_menu)
my_menu2.add_cascade(label="DATABASE",menu = database_menu)
database_menu.add_command(label = "Create")
database_menu.add_command(label = "Use")
database_menu.add_command(label = "Drop")

table_menu = Menu(my_menu)
my_menu2.add_cascade(label="TABLE",menu=table_menu)
table_menu.add_command(label= "Create")
table_menu.add_command(label= "Modify")
table_menu.add_command(label= "Drop")


#______Defining the root window__________

root = Tk()
root.geometry("1920x1080")
root.title("PyDBMS")
Expand All @@ -49,16 +141,18 @@ def file_dialog_box():
foo_frame = Frame(root,width=1920,height=30,bg="gray19").place(x=0,y=60,anchor="nw")
querry_frame = Frame(root,width=480,height=1020,bg="gray20").place(x=0,y=0,anchor="nw")

#toolbar
conn_button = Button(toolbar_frame,padx=2,text="CONNECT",font = ("Arial",12),command = connector_window,bg="gray27",fg="white",highlightthickness = 0,relief=RAISED).pack(anchor="nw")

#______Creating a toolbar for root window_________


root.option_add('*tearOff', FALSE)
my_menu = Menu(toolbar_frame,bg="gray30",fg="White",font=("Calibri",12),relief=RAISED)
root.config(menu = my_menu)

file_menu = Menu(my_menu)
my_menu.add_cascade(label = "FILE",menu = file_menu)
file_menu.add_command(label = "New")
file_menu.add_command(label = "New",command=newfileframe)
file_menu.add_command(label = "Open File",command = file_dialog_box)
file_menu.add_command(label = "Save as")
file_menu.add_command(label = "Save")
Expand All @@ -76,5 +170,7 @@ def file_dialog_box():
table_menu.add_command(label= "Modify")
table_menu.add_command(label= "Drop")

#initiate

#____main_____

root.mainloop()

0 comments on commit 9d9cb36

Please sign in to comment.