def show_database():
import [Link]
try:
db = [Link](host = "localhost", user = "root", password =
"ANTHONY")
cursor = [Link]()
[Link]("show databases")
for x in cursor:
print(x)
except:
print("Error in connection")
def create_table():
import [Link]
try:
db = [Link](host = "localhost",user = "root", password =
"ANTHONY", database = "school")
cursor = [Link]()
[Link]("create table student1(rollno int primary key,\name
varchar(20), stream varchar(10), total\int, grade varchar(3), Class int)")
print("Table created")
except:
print("Error in connection")
def showtables():
import [Link]
try:
db = [Link](host = "localhost",user = "root", password =
"ANTHONY", database = "school")
cursor = [Link]()
[Link]("show tables")
for x in cursor:
print(x)
except:
print("Error in connection")
def display_struc():
import [Link]
try:
db = [Link](host = "localhost",user = "root", password =
"ANTHONY", database = "school")
cursor = [Link]()
[Link]("desc student1")
for x in cursor:
print(x)
except:
print("Error in connection")
def add_rec():
import [Link]
try:
db = [Link](host = "localhost",user = "root", password =
"ANTHONY", database = "school")
cursor = [Link]()
rno = int(input("Enter roll number"))
nm = input("Enter name")
st = input("Enter stream")
tot = int(input("Enter total"))
gr = input("Enter grade")
C = int(input ("Enter class"))
sql_query = "insert into student1 values(%s,%s,%s,%s,%s,%s)"
val = (rno,nm,st,tot,gr,C)
[Link](sql_query,val)
[Link]()
print("Record added")
except:
[Link]()
print("Error, Record not added")
def update_rec():
import [Link]
try:
db = [Link](host = "localhost",user = "root", password =
"ANTHONY", database = "school")
cursor = [Link]()
rno = int(input("Enter rollno"))
tot = int(input("Enter total"))
sql_query = "Update student1 set total = %s where rollno = %s"
val = (tot,rno)
[Link](sql_query,val)
print([Link], "record updated")
[Link]()
except:
[Link]()
print("Record not updated")
def delete_rec():
import [Link]
try:
db = [Link](host = "localhost",user = "root", password =
"ANTHONY", database = "school")
cursor = [Link]()
rno = int(input("Enter rollno"))
sql_query = "delete from student1 where rollno = %s"
[Link](sql_query,(rno,))
print([Link], "record deleted")
[Link]()
except:
[Link]()
print("Record not deleted")
def fetch_data():
import [Link]
try:
db = [Link](host = "localhost", user = "root", password =
"ANTHONY", database = "school")
cursor = [Link]()
[Link]("SELECT * FROM student1")
records = [Link]()
for x in records:
print(x[0],x[1],x[2],x[3],x[4],x[5])
except:
print("Error, unable to fetch data")
def topper_list():
import [Link]
try:
db = [Link](host = "localhost",user = "root", password =
"", database = "school")
cursor = [Link]()
[Link]("select * from student1 order by total desc")
records = [Link]()
for x in records:
print(x[0],x[1],x[2],x[3],x[4],x[5])
except:
print("Error, unable to sort")
def main_menu():
print("STUDENT REGISTRATION SYSTEM")
print("1: To show databases")
print("2: To create a table")
print("3: To show tables present in the database")
print("4: To display structure of the table")
print("5: To add a record in the table")
print("6: To update a record in the table")
print("7: To delete a record from the table")
print("8: To display all the records from the table")
print("9: To sort the data in descending order of total")
print("10: To quit")
choice = int(input("Enter your choice"))
if choice == 1:
show_database()
elif choice == 2:
create_table()
elif choice == 3:
showtables()
elif choice == 4:
display_struc()
elif choice == 5:
add_rec()
elif choice == 6:
update_rec()
elif choice == 7:
delete_rec()
elif choice == 8:
fetch_data()
elif choice == 9:
topper_list()
elif choice == 10:
print("Exiting")
else:
print("Wrong input")
main_menu()