3
Question The code given below accepts the roll number of a student and deletes the record of that student from the table STUDENT. The structure of a record of table Student is: RollNo — integer; Name — string; Class — integer; Marks — integer.
Note the following to establish connectivity between Python and MySQL:
• Username is root, Password is A5_b23.
• The table exists in a MySQL database named SCHOOL.
Write the following missing statements to complete the code:
#Statement 1— to create the cursor object
#Statement 2 — to execute the query
#Statement 3 — to make the deletion in the database permanent
import mysql.connector as mysql
def sql data():
conn=mysql.connect(host="localhost",
user="root",
password=" ",
database="school")
............... #Statement 1
rno=int(input("Enter Roll Number : "))
qry="delete from student where RollNo={}".format(rno)
............... #Statement 2
............... #Statement 3
print ("Record deleted")#Statement 1 — cursor = conn.cursor()
#Statement 2 — cursor.execute(qry)
#Statement 3 — conn.commit()
The completed code is as follows:
import mysql.connector as mysql
def sql_data():
conn = mysql.connect(host = "localhost",
user = "root",
password = "A5_b23",
database = "school")
cursor = conn.cursor() # Statement 1 - to create the cursor object
rno = int(input("Enter Roll Number: "))
qry = "DELETE FROM student WHERE RollNo={}".format(rno)
cursor.execute(qry) # Statement 2 - to execute the query
conn.commit() # Statement 3 - to make the deletion in the database permanent
print("Record deleted")