You don’t want to write the instructions which are in red colour.
Program number you
can start from the last program’s next number.
Program 6 : Write a MySQL connectivity program in Python to display the details of
student table in database school :-
(Note: The student table is existing in database school)
import [Link] as ms
mycon=[Link](host="localhost", user="root", passwd="class12@2022", database="school")
if mycon.is_connected():
print("Successfully Connected")
cr=[Link]()
[Link]("select * from student")
data=[Link]()
for i in data:
print(i)
print("Records in list format is: ", data)
Output (Write on the output side)
Suppose the student table contains the records:
Successfully Connected
(101, 'Karthik', 'Yadav', 98)
(103, 'Mahima', 'Chaudhari', 89)
(104, 'Kavya', 'Singhal', 77)
(105, 'Kunal', 'Chauhan', 96)
Records in list format is: [(101, 'Karthik', 'Yadav', 98), (103, 'Mahima', 'Chaudhari',
89), (104, 'Kavya', 'Singhal', 77), (105, 'Kunal', 'Chauhan', 96)]
Program 7 : Write a MySQL connectivity program using function in Python to insert
rows to the table student in database school :-
1
(Note: The student table is already existing in database school)
import [Link] as ms
def enter_data():
mycon=[Link](host="localhost",user="root",passwd="class12@2022",database="school")
if mycon.is_connected():
print("Successfully Connected")
cr=[Link]()
while True:
rno=int(input("Enter the roll no:"))
fname=input("Enter the first name:")
lname=input("Enter the last name:")
mark=int(input("Enter the mark:"))
[Link]("insert into student values({},'{}','{}',{})".format(rno,fname,lname,mark))
[Link]()
ch=input("Do you want to enter records?")
if ch in "Nn":
break
[Link]("select * from student")
data=[Link]()
print("Data in list format is", data)
[Link]()
enter_data()
Output (Write on the output side)
Successfully Connected
Enter the roll no:106
Enter the first name:Ajay
Enter the last name:Saxena
2
Enter the mark:80
Do you want to enter records?y
Enter the roll no:108
Enter the first name:Raghav
Enter the last name:Agnihotri
Enter the mark:66
Do you want to enter records?n
Data in list format is [(101, 'Karthik', 'Yadav', 98), (103, 'Mahima', 'Chaudhari', 89),
(104, 'Kavya', 'Singhal', 77), (105, 'Kunal', 'Chauhan', 96), (106, 'Ajay', 'Saxena',
80), (108, 'Raghav', 'Agnihotri', 66)]
Program 8 : Write a MySQL connectivity program using function in Python to update
the mark of a student by accepting the roll number of the student :-
(Note: The student table is already existing in database school)
import [Link] as ms
def update_data():
mycon=[Link](host="localhost",user="root",passwd="class12@2022",database="school")
if mycon.is_connected():
print("Successfully Connected")
cr=[Link]()
[Link]("select * from student")
data=[Link]()
print("Data in list format before updating : ", data)
rno=int(input("Enter the roll no of the student to update the data:"))
mark=int(input("Enter the new mark to be updated:"))
[Link]("update student set mark={} where roll_no={}".format(mark,rno))
3
[Link]()
[Link]("select * from student")
data=[Link]()
print("Data in list format after updating: ", data)
[Link]()
update_data()
Output (Write on the output side)
Successfully Connected
Data in list format before updating : [(101, 'Karthik', 'Yadav', 98), (103, 'Mahima',
'Chaudhari', 89), (104, 'Kavya', 'Singhal', 77), (105, 'Kunal', 'Chauhan', 96), (106, 'Ajay',
'Saxena', 80), (108, 'Raghav', 'Agnihotri', 66)]
Enter the roll no of the student to update the data:101
Enter the new mark to be updated:99
Data in list format after updating: [(101, 'Karthik', 'Yadav', 99), (103, 'Mahima', 'Chaudhari',
89), (104, 'Kavya', 'Singhal', 77), (105, 'Kunal', 'Chauhan', 96), (106, 'Ajay', 'Saxena', 80),
(108, 'Raghav', 'Agnihotri', 66)]
Program 9 : Write a MySQL connectivity program using function in Python to delete
the details of a student by accepting the roll number of the student :-
(Note: The student table is already existing in database school)
import [Link] as ms
def delete_data():
mycon=[Link](host="localhost",user="root",passwd="class12@2022",database="school")
if mycon.is_connected():
print("Successfully Connected")
cr=[Link]()
[Link]("select * from student")
4
data=[Link]()
print("Data in list format before deleting : ", data)
rno=int(input("Enter the roll no of the student to delete the data:"))
[Link]("delete from student where roll_no={}".format(rno))
[Link]()
[Link]("select * from student")
data=[Link]()
print("Data in list format after deleting: ", data)
[Link]()
delete_data()
Output (Write on the output side)
Successfully Connected
Data in list format before deleting : [(101, 'Karthik', 'Yadav', 99), (103, 'Mahima',
'Chaudhari', 89), (104, 'Kavya', 'Singhal', 77), (105, 'Kunal', 'Chauhan', 96), (106, 'Ajay',
'Saxena', 80), (108, 'Raghav', 'Agnihotri', 66)]
Enter the roll no of the student to delete the data:106
Data in list format after deleting: [(101, 'Karthik', 'Yadav', 99), (103, 'Mahima', 'Chaudhari',
89), (104, 'Kavya', 'Singhal', 77), (105, 'Kunal', 'Chauhan', 96), (108, 'Raghav', 'Agnihotri',
66)]
NB:
1. You should fill in the certificate page including your photo.
2. Fill in Index Page also(SQL programs).
a. Date for Programs 1 ,2 –1/11/2023
b. Date for Programs 3 –2/11/2023
c. Date for Programs 4 , 5 - 3/11/2023
d. Date for program 6 - 7/11/2023
e. Date for program 7 - 8/11/2023
f. Date for programs 8 , 9 -9/11/2023
Ashalakshmi.R
(15.11.2023)
5