Exception Handling
try:
print("TRY BLOCK PROGRAM STARTED")
import pymysql
# Connect to the database
conn = pymysql.connect(host='localhost',user='root',password='aaditya2007',)
# Create a cursor object
cur = conn.cursor()
#creating database
cur.execute('create database if not exists sports;')
cur.execute('use sports;')
#creating Table
cur.execute('create table if not exists cricket(s_id int(3),Name varchar(100),Country varchar
(100),Role varchar(20),Debut int(4));')
# Insert 15 records into the table
cur.execute('''insert into cricket values
(891,"Sachin Tendulkar", "India", "Batsman", 1989),
(289,"Jacques Kallis", "South Africa", "All-rounder", 1995),
(834,"Rahul Dravid", "India", "Batsman", 1996),
(800,"Shane Warne", "Australia", "Bowler", 1992),
(213,"Muttiah Muralitharan", "Sri Lanka", "Bowler", 1992),
(675,"Brian Lara", "West Indies", "Batsman", 1990),
(503,"Wasim Akram", "Pakistan", "Bowler", 1985),
(461,"Allan Donald", "South Africa", "Bowler", 1992),
(432,"Vivian Richards", "West Indies", "Batsman", 1974),
(378,"Sunil Gavaskar", "India", "Batsman", 1971),
(999,"Ian Botham", "England", "All-rounder", 1977),
(309,"Kapil Dev", "India", "All-rounder", 1978),
(844,"Malcolm Marshall", "West Indies", "Bowler", 1978),
(640,"Dennis Lillee", "Australia", "Bowler", 1970),
(562,"Richard Hadlee", "New Zealand", "Bowler", 1973);''')
#Functions for 'transport' table
def add_cricket():
import random
x=random.randint(100,999)
print("Cricket id is ",x)
s_id=x
name=input("Enter name of the cricketer : ")
country=input("Enter country of the cricketer :")
role=input("Enter role of the cricketer : ")
debut=int(input("Enter debut year of cricketer : "))
sol="insert into cricket values('%d','%s','%s','%s','%d')"%(s_id,name,country,role,debut)
cur.execute(sol)
conn.commit()
print('updated successfully')
g=cur.execute("select * from cricket;")
h=cur.fetchall()
for i in h:
print(i)
def update_name():
s_id=int(input("Enter cricket Id :"))
name=input("Enter updated cricketer name : ")
sol="update cricket set name='%s' where s_id='%d' "%(name,s_id)
cur.execute(sol)
conn.commit()
print('updated successfully')
query="select * from cricket where s_id='%d' "%(s_id)
cur.execute(query)
h=cur.fetchall()
for i in h:
print(i)
def update_country():
s_id=int(input("Enter cricket Id :"))
country=input("Enter updated cricketer's counrty : ")
sol="update cricket set country='%s' where s_id='%d' "%(country,s_id)
cur.execute(sol)
conn.commit()
print('updated successfully')
query="select * from cricket where s_id='%d' "%(s_id)
cur.execute(query)
h=cur.fetchall()
for i in h:
print(i)
def update_role():
s_id=int(input("Enter cricket Id :"))
role=input("Enter updated cricketer's role : ")
sol="update cricket set role='%s' where s_id='%d' "%(role,s_id)
cur.execute(sol)
conn.commit()
print('updated successfully')
query="select * from cricket where s_id='%d' "%(s_id)
cur.execute(query)
h=cur.fetchall()
for i in h:
print(i)
def update_debut():
s_id=int(input("Enter cricket Id :"))
debut=int(input("Enter updated cricketer's debut year : "))
sol="update cricket set debut='%d' where s_id='%d' "%(debut,s_id)
cur.execute(sol)
conn.commit()
print('updated successfully')
query="select * from cricket where s_id='%d' "%(s_id)
cur.execute(query)
h=cur.fetchall()
for i in h:
print(i)
def delete_cricket():
s_id=int(input("Enter cricket id to be removed :"))
sol="delete from cricket where s_id='%d' "%(s_id)
cur.execute(sol)
conn.commit()
print('Removed successfully')
def search_cricket():
s_id=int(input("Enter cricket id to search :"))
sol="select * from cricket where s_id='%d' "%(s_id)
cur.execute(sol)
h=cur.fetchall()
k=0
for i in h:
k=1
if k==1:
print("Yes this cricket id exists")
print(i)
if k==0:
print("No this cricket id doesn't exist")
#FUNCTIONS DEFINED TO UPDATE DATA OF CRICKETERS
def update_data():
while True:
print("What do you want to update sir")
print("1.Update cricketer's name")
print("2.Update cricket's country ")
print("3.Update cricketer's role")
print("4.Update cricketer's debut year")
print("5.Get Back")
z=int(input("May I know your need SIR : "))
if z==1:
update_name()
elif z==2:
update_country()
elif z==3:
update_role()
elif z==4:
update_debut()
elif z==5 :
print("Thank you coming sir")
break
else :
print("invalid choice sir , please try again")
continue
#USER INTERFACE
a=input("Enter your name : ")
print("Welcome",a,"to our program [CRICKET]")
while True:
print("Kindly provide us your reason to visit by telling the serial number of following things
")
print("1.View the data of cricketers")
print("2.Add the data of cricketers")
print("3.Update the data of cricketers")
print("4.Search the data of cricketers")
print("5.Delete a data of cricketers")
print("6.Exit")
y=int(input("Enter serial number : "))
if y==1:
g=cur.execute("select * from cricket;")
h=cur.fetchall()
for i in h:
print(i)
elif y==2:
add_cricket()
elif y==3:
update_data()
elif y==4:
search_cricket()
elif y==5:
delete_cricket()
elif y==6:
print("Thank you for coming")
break
else:
print("Invalid choice")
continue
except ValueError:
print("VALUE ERROR OCCURED")
except pymysql.Error as e:
print("ERROR OCCURED IN SQL")
print(e)
finally:
print("TRY BLOCK PROGRAM FINISHED")
OUTPUT
TRY BLOCK PROGRAM STARTED
Enter your name : sumit
Welcome sumit to our program [CRICKET]
Kindly provide us your reason to visit by telling the serial number of following things
1.View the data of cricketers
2.Add the data of cricketers
3.Update the data of cricketers
4.Search the data of cricketers
5.Delete a data of cricketers
6.Exit
Enter serial number : 1
(891, 'Sachin Tendulkar', 'India', 'Batsman', 1989)
(289, 'Jacques Kallis', 'South Africa', 'All-rounder', 1995)
(834, 'Rahul Dravid', 'India', 'Batsman', 1996)
(800, 'Shane Warne', 'Australia', 'Bowler', 1992)
(213, 'Muttiah Muralitharan', 'Sri Lanka', 'Bowler', 1992)
(675, 'Brian Lara', 'West Indies', 'Batsman', 1990)
(503, 'Wasim Akram', 'Pakistan', 'Bowler', 1985)
(461, 'Allan Donald', 'South Africa', 'Bowler', 1992)
(432, 'Vivian Richards', 'West Indies', 'Batsman', 1974)
(378, 'Sunil Gavaskar', 'India', 'Batsman', 1971)
(999, 'Ian Botham', 'England', 'All-rounder', 1977)
(309, 'Kapil Dev', 'India', 'All-rounder', 1978)
(844, 'Malcolm Marshall', 'West Indies', 'Bowler', 1978)
(640, 'Dennis Lillee', 'Australia', 'Bowler', 1970)
(562, 'Richard Hadlee', 'New Zealand', 'Bowler', 1973)
Kindly provide us your reason to visit by telling the serial number of following things
1.View the data of cricketers
2.Add the data of cricketers
3.Update the data of cricketers
4.Search the data of cricketers
5.Delete a data of cricketers
6.Exit
Enter serial number : 2
Cricket id is 574
Enter name of the cricketer : Virat Kohli
Enter country of the cricketer :India
Enter role of the cricketer : Batsman
Enter debut year of cricketer : 2006
updated successfully
(891, 'Sachin Tendulkar', 'India', 'Batsman', 1989)
(289, 'Jacques Kallis', 'South Africa', 'All-rounder', 1995)
(834, 'Rahul Dravid', 'India', 'Batsman', 1996)
(800, 'Shane Warne', 'Australia', 'Bowler', 1992)
(213, 'Muttiah Muralitharan', 'Sri Lanka', 'Bowler', 1992)
(675, 'Brian Lara', 'West Indies', 'Batsman', 1990)
(503, 'Wasim Akram', 'Pakistan', 'Bowler', 1985)
(461, 'Allan Donald', 'South Africa', 'Bowler', 1992)
(432, 'Vivian Richards', 'West Indies', 'Batsman', 1974)
(378, 'Sunil Gavaskar', 'India', 'Batsman', 1971)
(999, 'Ian Botham', 'England', 'All-rounder', 1977)
(309, 'Kapil Dev', 'India', 'All-rounder', 1978)
(844, 'Malcolm Marshall', 'West Indies', 'Bowler', 1978)
(640, 'Dennis Lillee', 'Australia', 'Bowler', 1970)
(562, 'Richard Hadlee', 'New Zealand', 'Bowler', 1973)
(574, 'Virat Kohli', 'India', 'Batsman', 2006)
Kindly provide us your reason to visit by telling the serial number of following things
1.View the data of cricketers
2.Add the data of cricketers
3.Update the data of cricketers
4.Search the data of cricketers
5.Delete a data of cricketers
6.Exit
Enter serial number : 3
What do you want to update sir
1.Update cricketer's name
2.Update cricket's country
3.Update cricketer's role
4.Update cricketer's debut year
5.Get Back
May I know your need SIR : 1
Enter cricket Id :574
Enter updated cricketer name : Joe Root
updated successfully
(574, 'Joe Root', 'India', 'Batsman', 2006)
What do you want to update sir
1.Update cricketer's name
2.Update cricket's country
3.Update cricketer's role
4.Update cricketer's debut year
5.Get Back
May I know your need SIR : 2
Enter cricket Id :574
Enter updated cricketer's counrty : England
updated successfully
(574, 'Joe Root', 'England', 'Batsman', 2006)
What do you want to update sir
1.Update cricketer's name
2.Update cricket's country
3.Update cricketer's role
4.Update cricketer's debut year
5.Get Back
May I know your need SIR : 3
Enter cricket Id :574
Enter updated cricketer's role : All Rounder
updated successfully
(574, 'Joe Root', 'England', 'All Rounder', 2006)
What do you want to update sir
1.Update cricketer's name
2.Update cricket's country
3.Update cricketer's role
4.Update cricketer's debut year
5.Get Back
May I know your need SIR : 4
Enter cricket Id :574
Enter updated cricketer's debut year : 2005
updated successfully
(574, 'Joe Root', 'England', 'All Rounder', 2005)
What do you want to update sir
1.Update cricketer's name
2.Update cricket's country
3.Update cricketer's role
4.Update cricketer's debut year
5.Get Back
May I know your need SIR : 5
Thank you coming sir
Kindly provide us your reason to visit by telling the serial number of following things
1.View the data of cricketers
2.Add the data of cricketers
3.Update the data of cricketers
4.Search the data of cricketers
5.Delete a data of cricketers
6.Exit
Enter serial number : 4
Enter cricket id to search :574
Yes this cricket id exists
(574, 'Joe Root', 'England', 'All Rounder', 2005)
Kindly provide us your reason to visit by telling the serial number of following things
1.View the data of cricketers
2.Add the data of cricketers
3.Update the data of cricketers
4.Search the data of cricketers
5.Delete a data of cricketers
6.Exit
Enter serial number : 4
Enter cricket id to search :574
Yes this cricket id exists
(574, 'Joe Root', 'England', 'All Rounder', 2005)
Kindly provide us your reason to visit by telling the serial number of following things
1.View the data of cricketers
2.Add the data of cricketers
3.Update the data of cricketers
4.Search the data of cricketers
5.Delete a data of cricketers
6.Exit
Enter serial number : 5
Enter cricket id to be removed :574
Removed successfully
Kindly provide us your reason to visit by telling the serial number of following things
1.View the data of cricketers
2.Add the data of cricketers
3.Update the data of cricketers
4.Search the data of cricketers
5.Delete a data of cricketers
6.Exit
Enter serial number : 6
Thank you for coming
TRY BLOCK PROGRAM FINISHED
EXCEPTION OUTPUT
TRY BLOCK PROGRAM STARTED
Enter your name : sumit
Welcome sumit to our program [CRICKET]
Kindly provide us your reason to visit by telling the serial number of following things
1.View the data of cricketers
2.Add the data of cricketers
3.Update the data of cricketers
4.Search the data of cricketers
5.Delete a data of cricketers
6.Exit
Enter serial number : sumit
VALUE ERROR OCCURED
TRY BLOCK PROGRAM FINISHED