import mysql.
connector
mycon=[Link](host="localhost",user="root",passwd="root123")
if mycon.is_connected():
print("Connection Established")
else:
print("Error in connection")
c1=[Link]()
[Link]("create database if not exists learn")
[Link]("use learn")
def tablecreate():
c1=[Link]() # Cursor created
[Link]("create table if not exists student\
(\
rollno int(3) primary key,\
name varchar(20),\
class varchar(5),\
sec char(1), marks integer(3))")
print("Table created")
def insertrecord():
c1=[Link]()
# single row values at run time
r=int(input("enter rollno:"))
n=input("enter name:")
c=input("enter class:")
s=input("enter section:")
m=int(input("enter marks:"))
Page 1.1
m=int(input("enter marks:"))
sq="insert into student(rollno,name,class,sec,marks)values(%s,%s,%s,%s,%s)"
a=(r,n,c,s,m)
[Link](sq,a)
[Link]()
print("Row inserted")
def updaterecord():
c1=[Link]()
r=int(input("enter roll no:")) #updation of values at run time
print("What you want to update?")
print("1. Name of Student")
print("2. Class of Student")
print("3. Section of Student")
print("4. Marks of Student")
ch=int(input("Enter Your Choice:"))
if ch==1:
na=input("enter Name to be updated:")
k=(na,r)
q="update student set name=%s where rollno=%s"
[Link](q,k)
[Link]()
print("Name Updated")
elif ch==2:
cl=input("Enter Class to be updated:")
k=(cl,r)
q="update student set class=%s where rollno=%s"
[Link](q,k)
[Link]()
print("Class Updated")
elif ch==3:
sec=input("Enter Section to be updated:")
k=(sec,r)
Page 1.2
k=(sec,r)
q="update student set sec=%s where rollno=%s"
[Link](q,k)
[Link]()
print("Section Updated")
elif ch==4:
m=input("Enter Marks to be updated:")
k=(m,r)
q="update student set marks=%s where rollno=%s"
[Link](q,k)
[Link]()
print("Marks Updated")
def deleterecord():
c1=[Link]()
r=int(input("enter roll no:"))
a=(r,)
q="Delete from student where rollno=%s"
[Link](q,a)
[Link]()
print("Record Deleted")
def readrecord():
c1=[Link]()
print("What you want to retreive?")
print("1. Single Record")
print("2. Specific No of Rows")
print("3. Whole Table")
print("4. Specific records based on Condition")
ch=int(input("Enter Your Choice"))
if ch==1:
s="Select * from Student"
[Link](s)
Page 1.3
[Link](s)
r=[Link]()
print(r)
elif ch==2:
s=int(input("How many rows You want to display?:"))
q="Select * from student"
[Link](q)
r=[Link](s)
for i in r:
print(i)
elif ch==3:
q="Select * from student"
[Link](q)
r=[Link]()
for i in r:
print(i)
elif ch==4:
c=input("Enter Class in Romans:")
q="Select * from student where class=%s";
a=(c,)
[Link](q,a);
r=[Link]()
print("========================================")
for x in r:
print(x)
def main_menu():
print("Database Operations")
print("====================")
print("1. Table Creation");
print("2. Insert Records");
Page 1.4
print("2. Insert Records");
print("3. Update Records");
print("4. Delete Records");
print("5. Show Records");
print("6. Exit")
ch=int(input("Enter Your Choice:"))
if ch==1:
tablecreate()
elif ch==2:
insertrecord()
elif ch==3:
updaterecord()
elif ch==4:
deleterecord()
elif ch==5:
readrecord()
elif ch==6:
exit()
main_menu()
Page 1.5