Question:medium

Write a function Delete_Theatre() to input the value of Th_ID from the user and permanently delete the corresponding record from the THEATRE table in the CINEMA database.

Given:
Host: localhost, User: root, Password: Ex2025

Show Hint

Always use commit() after INSERT, UPDATE, or DELETE to save changes.
Use cursor.rowcount to check if a record was modified or deleted.
For security and reliability, use parameterized queries like %s to avoid SQL injection.
Updated On: Jan 14, 2026
Show Solution

Solution and Explanation

Python Function:

Code:


import mysql.connector

def Delete_Theatre():
    try:
        mydb = mysql.connector.connect(
            host="localhost",
            user="root",
            passwd="Ex2025",
            database="CINEMA"
        )

        mycursor = mydb.cursor()

        thid = input("Enter Theatre ID to delete: ")
        query = "DELETE FROM THEATRE WHERE Th_ID = %s"
        mycursor.execute(query, (thid,))
        mydb.commit()

        if mycursor.rowcount > 0:
            print("Record deleted successfully.")
        else:
            print("No record found with the given Theatre ID.")

        mycursor.close()
        mydb.close()

    except mysql.connector.Error as err:
        print("Error:", err)
  

Explanation:

  • Establishes a connection to the CINEMA database.
  • Executes a SQL DELETE statement on the THEATRE table using the provided Th_ID.
  • Commits the transaction and checks whether any rows were affected.
  • Displays a success or failure message accordingly.
  • Handles database errors gracefully using exception handling.
Was this answer helpful?
0