0% found this document useful (0 votes)
17 views

SQL Record Questions Part2

The document contains 9 Python programs that connect to a MySQL database called "school" and perform various operations like displaying, inserting, updating, and deleting records from the student table. Program 6 displays all records from the student table. Program 7 inserts new records into the student table by taking input from the user. Program 8 updates the mark of a student by roll number. Program 9 deletes a student record by roll number.

Uploaded by

mishrapratham169
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

SQL Record Questions Part2

The document contains 9 Python programs that connect to a MySQL database called "school" and perform various operations like displaying, inserting, updating, and deleting records from the student table. Program 6 displays all records from the student table. Program 7 inserts new records into the student table by taking input from the user. Program 8 updates the mark of a student by roll number. Program 9 deletes a student record by roll number.

Uploaded by

mishrapratham169
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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 mysql.connector as ms
mycon=ms.connect(host="localhost", user="root", passwd="class12@2022", database="school")
if mycon.is_connected():
print("Successfully Connected")
cr=mycon.cursor()
cr.execute("select * from student")
data=cr.fetchall()
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 mysql.connector as ms

def enter_data():

mycon=ms.connect(host="localhost",user="root",passwd="class12@2022",database="school")

if mycon.is_connected():

print("Successfully Connected")

cr=mycon.cursor()

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:"))

cr.execute("insert into student values({},'{}','{}',{})".format(rno,fname,lname,mark))

mycon.commit()

ch=input("Do you want to enter records?")

if ch in "Nn":

break

cr.execute("select * from student")

data=cr.fetchall()

print("Data in list format is", data)

mycon.close()

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 mysql.connector as ms

def update_data():

mycon=ms.connect(host="localhost",user="root",passwd="class12@2022",database="school")

if mycon.is_connected():

print("Successfully Connected")

cr=mycon.cursor()

cr.execute("select * from student")

data=cr.fetchall()

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:"))

cr.execute("update student set mark={} where roll_no={}".format(mark,rno))

3
mycon.commit()

cr.execute("select * from student")

data=cr.fetchall()

print("Data in list format after updating: ", data)

mycon.close()

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 mysql.connector as ms

def delete_data():

mycon=ms.connect(host="localhost",user="root",passwd="class12@2022",database="school")

if mycon.is_connected():

print("Successfully Connected")

cr=mycon.cursor()

cr.execute("select * from student")


4
data=cr.fetchall()

print("Data in list format before deleting : ", data)

rno=int(input("Enter the roll no of the student to delete the data:"))

cr.execute("delete from student where roll_no={}".format(rno))

mycon.commit()

cr.execute("select * from student")

data=cr.fetchall()

print("Data in list format after deleting: ", data)

mycon.close()

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

You might also like