Test Index

CBSE Class 12 Computer Science 2025 Paper Set 4 Get it on Google PlayDownload on the App Store

© examsnet.com
Question : 35 of 37
Marks: +1, -0
A table, named THEATRE, in CINEMA database, has the following structure :
FieldType
Th_IDchar(5)
Namevarchar(15)
Cityvarchar(15)
Locationvarchar(15)
Seatsint
Write a function Delete_Theatre(), to input the value of Th_ID from the user and permanently delete the corresponding record from the table.
Assume the following for Python-Database connectivity :
Host : localhost, User : root, Password : Ex2025
Solution:  
Concept: Steps of Python-MySQL connectivity: import the connector, connect to the CINEMA database, create a cursor, execute the DELETE query, and call commit() so that the deletion is saved permanently, then close the connection.
import mysql.connector as mycon
def Delete_Theatre():
con = mycon.connect(host="localhost", user="root",
password="Ex2025", database="CINEMA")
cur = con.cursor()
tid = input("Enter Th_ID of the theatre to delete: ")
query = "DELETE FROM THEATRE WHERE Th_ID = '{}'".format(tid)
cur.execute(query)
con.commit() # makes the delete permanent
print(cur.rowcount, "record(s) deleted")
con.close()
Delete_Theatre()
Explanation:
• Th_ID is char(5), so its value is put inside quotes in the query.
• The query can also be written with a placeholder: cur.execute("DELETE FROM THEATRE WHERE Th_ID = %s", (tid,)).
• Without con.commit() the deletion is not saved in the table.
© examsnet.com
Advertisements
Go to Question: