CS Project File
CS Project File
#Modules
pas = "benihime09"
#Required Functions
def menu():
l = ["Create Table", "Manage Table", "Delete Table", "Display From Different Tables", "Exit"]
print("-----------------------------------------------------------------\nMain Menu")
for i in range(len(l)):
print("-----------------------------------------------------------------")
def sbmenu():
l = ["Modify Attributes", "Show Records", "Insert Record", "Update Record", "Delete Record",
"Exit"]
for i in range(len(l)):
print("-----------------------------------------------------------------\n")
def altmenu():
for i in range(len(l)):
def dispmenu():
l = ["Display all", "Display all in order", "Display specific", "Display specific in order"]
print("-----------------------------------------------------------------\nDisplay Menu")
for i in range(len(l)):
print("-----------------------------------------------------------------")
def modmenu():
for i in range(len(l)):
print("-----------------------------------------------------------------")
def chk_tb(tb):
cur.execute("show tables;")
tblst = cur.fetchall()
for i in range(len(tblst)):
if tb == tblst[i][0]:
return True
else:
return False
def shw_tb():
cur.execute(que)
tblst = cur.fetchall()
print("Available Tables:")
for i in range(len(tblst)):
print(str(i+1)+"." ,tblst[i][0])
def cr_tb(tb):
try:
print("Error!!!")
print(err)
else:
for i in range(n):
if i != n-1:
que += name + " " + dat + " " + par + ", "
continue
else:
continue
try:
cur.execute(que)
pc.rollback()
print(err)
else:
print("\nTable Created!!")
pc.commit()
#9. Display
def dis():
lst = []
col = cur.column_names
rec = cur.fetchall()
for i in rec:
d = {}
for j in range(len(col)):
d[col[j]] = i[j]
print(d)
lst.append(d)
continue
return lst
def colnam(tb):
r = cur.fetchall()
col = cur.column_names
cols = list(col)
return cols
print("-----------------------------------------------------------------\nAvailable Columns:")
for i in range(len(cols)):
chcklst.append(str(i+1))
dic[i+1] = cols[i]
print(str(i+1)+". "+cols[i])
print(f"{int(chcklst[-1])+1}. Exit\n-----------------------------------------------------------------")
def corr(lst):
c=0
for i in range(len(lst)):
c += 1
lst[i] = "Null"
return lst
#######################################################
#Main
try:
print("\n"*30+"Not Connecting")
print(err)
else:
cur = pc.cursor()
while run:
menu()
shw_tb()
#Creating Table
if choi == "1":
if chk_tb(tb):
c=0
else:
cr_tb(tb)
#Manage Table
tbchk = chk_tb(tab)
if tbchk:
while tbchk:
sbmenu()
##Alter Table
if sbchoi == "1":
rundis = True
while rundis:
altmenu()
###Adding Column
if altchoi == "1":
colst = colnam(tab)
chklst = []
d = {}
print("-----------------------------------------------------------------\nAvailable Columns:")
for i in range(len(colst)):
chklst.append(str(i+1))
d[i+1] = colst[i]
print(str(i+1)+". "+colst[i])
print("\n-----------------------------------------------------------------")
ncolst = []
for i in colst:
x = i.lower()
ncolst.append(x)
que = "alter table " + tab + " add (" + name + " " + dat + " " + par + ");"
cur = pc.cursor()
try:
cur.execute(que)
except Exception as err:
print("\nERROR!!\n"+err)
else:
else:
else:
continue
###Modifying Column
if altchoi == "2":
colst = colnam(tab)
chklst = []
d = {}
runt = True
while runt:
print("\n")
colprint(colst, chklst, d)
if colmod in chklst:
modmenu()
if cmd == "1":
if nnam == "0":
continue
else:
rand = True
while rand:
print("Are you sure you wish to change from \'", d[int(colmod)], "\'
to \'", nnam, "\'?")
if con.lower() == "y":
cur = pc.cursor()
try:
print("alter table " + tab + " rename column " + d[int(colmod)] + "
to " + nnam + ";")
rand = False
runt = False
print("\nError!!\n"+err)
else:
print("\nAborting Renaming!!\n\n......\n......\nAborted!")
rand = False
runt = False
else:
print("Invalid Option Entered!")
que = "alter table " + tab + " modify column " + d[int(colmod)] + " " + dat +
";"
try:
cur = pc.cursor()
cur.execute(que)
runt = False
print("\nError!!\n"+err)
else:
continue
####Primary Key
cur = pc.cursor()
attlst = cur.fetchall()
go = True
for i in attlst:
if i[3].lower() == "pri":
go = False
else:
continue
if go:
que = "alter table " + tab + " add primary key (" + d[int(colmod)] + ");"
try:
cur = pc.cursor()
cur.execute(que)
print("\nError!!\n"+err)
else:
else:
continue
runt = False
continue
else:
continue
continue
####Exiting
continue
else:
continue
continue
###Deleting Column
if altchoi == "3":
colst = colnam(tab)
chklst = []
d = {}
colprint(colst, chklst, d)
if col in chklst:
delcol = d[int(col)]
con = input("Are you sure, you wish to delete column \'"+ delcol.upper() + "\'\
nYes or No(y/n): ")
if con.lower() == "y":
cur = pc.cursor()
que = "alter table " + tab + " drop column " + delcol + ";"
try:
cur.execute(que)
print("\n\n"+err)
else:
print("\nAborting Deletion!!\n\n......\n......\nAborted!")
continue
else:
continue
if altchoi == "4":
rundis = False
continue
else:
continue
##Display records
dispmenu()
###Display all
if dischoi == "1":
disp = dis()
d = {}
chklst = []
sel = True
colprint(colst, chklst, d)
while sel:
if dispby in chklst:
while True:
if orde.lower() == "a":
orde = "asc"
sel = False
break
orde = "desc"
sel = False
break
else:
continue
que = "select * from " + tab + " order by " + d[int(dispby)] + " " + orde + ";"
cur.execute(que)
disp = dis()
elif dispby == str(int(chklst[-1])+1):
sel = False
continue
else:
###Display specific
d = {}
chklst = []
cur = pc.cursor()
colst = colnam(tab)
colprint(colst, chklst, d)
sel = True
coldlst = []
while sel:
if coldisp in chklst:
coldlst.append(coldisp)
continue
sel = False
else:
cur = pc.cursor()
if len(coldlst) != 0:
for i in range(len(coldlst)):
que += d[int(coldlst[i])]
if i != len(coldlst)-1:
else:
cur.execute(que)
disp = dis()
else:
continue
continue
d = {}
chklst = []
cur = pc.cursor()
colst = colnam(tab)
colprint(colst, chklst, d)
sel = True
coldlst = []
while sel:
if coldisp in chklst:
coldlst.append(coldisp)
continue
sel = False
else:
cur = pc.cursor()
if len(coldlst) != 0:
for i in range(len(coldlst)):
que += d[int(coldlst[i])]
if i != len(coldlst)-1:
else:
sel = True
while sel:
if dispby in chklst:
while True:
if orde.lower() == "a":
orde = "asc"
sel = False
break
orde = "desc"
sel = False
break
else:
continue
else:
cur.execute(que)
disp = dis()
else:
continue
continue
###Invalid Input
else:
continue
##Insert a record
reclst = []
que = "select * from " + tab + ";"
colst = colnam(tab)
for i in colst:
if ent != "":
if type(ent) == type("s4"):
entin = ent
else:
entin = eval(ent)
else:
entin = ent
reclst.append(entin)
cur = pc.cursor()
val = corr(reclst)
for i in val:
if i != val[-1]:
else:
que += i + "');"
try:
cur.execute(que)
print("Error Occurred!\n"+err)
else:
print("Record Inserted!")
pc.commit()
continue
cur = pc.cursor()
disp = dis()
cur = pc.cursor()
attlst = cur.fetchall()
cur = pc.cursor()
colst = colnam(tab)
d = {}
chklst = []
colprint(colst, chklst, d)
cur = pc.cursor()
attlst = cur.fetchall()
for i in attlst:
if i[3].lower() == "pri":
att = i[0]
break
upkno = input("\nEnter attribute you want to update\nEnter option(1-" + str(int(chklst[-
1])+1) + "): ")
if upkno in chklst:
upkey = d[int(upkno)]
idlst = []
for i in disp:
idlst.append(i[att])
if idk in idlst:
for i in disp:
if i[att] == idk:
oval = i[upkey]
break
con = input("Are you sure, you wish to update record with " + att + " - \'" + idk +
"\'\nFrom \'" + str(oval) + "\' to \'" + str(nval) + "\'\nYes or No(y/n): ")
if con.lower() == "y":
cur = pc.cursor()
try:
if type(nval) == type("s4"):
cur.execute("update " + tab + " set " + upkey + "= \'" + nval + "\' where " + att
+ " = \'" + idk + "\';")
else:
nvalc = eval(nval)
cur.execute("update " + tab + " set " + upkey + " = " + nvalc + " where " + att
+ " = \'" + idk + "\';")
else:
else:
else:
continue
cur = pc.cursor()
disp = dis()
cur = pc.cursor()
attlst = cur.fetchall()
for i in attlst:
if i[3].lower() == "pri":
att = i[0]
break
attlst = []
for i in disp:
x = i[att]
attlst.append(x)
if delkey in attlst:
con = input("Are you sure, you wish to delete record with "+ att.upper() + " - \'"+
delkey +"\'\nYes or No(y/n): ")
if con.lower() == "y":
print("\nDeleting Record...")
que = "delete from " + tab + " where " + att + " = \'" + delkey + "\';"
try:
cur.execute(que)
print(que)
print("\nERROR!!")
print(err)
else:
print("\n......\nRecord Deleted")
break
print("\nAborting Deletion!!\n\n......\n......\nAborted!")
continue
else:
continue
tbchk = False
##Invalid Input
else:
print("\n")
else:
continue
#Delete Table
if not(chk_tb(tab)):
else:
while True:
con = input("\nAre you sure, you wish to delete table \'"+tab.upper()+"\'\n(*Data will not
be saved*)\nYes or No(y/n): ")
if con.lower() == "y":
print("\nDeleting Table...")
try:
cur.execute(que)
print("\nERROR!!")
print(err)
else:
print("\n......\nTable Deleted")
break
print("\nAborting Deletion!!\n\n......\n......\nAborted!")
continue
cur.execute("select SID, CName, sales.PID, PName, QTY, Price from stock, sales where sales.PID
= stock.PID;")
r = cur.fetchall()
coll = cur.column_names
colss = list(coll)
chlst = []
d = {}
print("-----------------------------------------------------------------\nAvailable Columns:")
for i in range(len(colss)):
chlst.append(str(i+1))
d[i+1] = colss[i]
print(str(i+1)+". "+colss[i])
print(f"{int(chlst[-1])+1}. Exit\n-----------------------------------------------------------------")
coldlst = []
sel = True
coldlst = []
while sel:
if coldisp in chlst:
coldlst.append(coldisp)
continue
sel = False
else:
for i in range(len(d)):
if d[i+1] == "PID":
d[i+1] = "sales.PID"
else:
continue
for i in coldlst:
if i != coldlst[-1]:
else:
que += d[int(i)]
try:
cur = pc.cursor()
cur.execute(que)
print("\nERROR!\n" + err)
else:
print("\n\n-----------------------------------------------------------------\nData:\n")
l = dis()
print("\n-----------------------------------------------------------------\n")
continue
#Exiting
run = False
print("\nThank You!")
#Invalid Input
else:
print("Exiting!!\n\n")