CBSE · 083Class XII · 2023Section D3 marks
Question 32(2)
30 March 2023 · Computer Science (083)
The code given below deletes the record from the table employee which has the structure :
text
E_code - String
E_name - String
Sal - Integer
City - StringNote: Username is root, Password is root, the table exists in MySQL database named emp.
Write the following statements to complete the code : - Statement 1 — to import the desired library. - Statement 2 — to execute the command that deletes the record with E_code as 'E101'. - Statement 3 — to delete the record permanently from the database.
python
import __________ as mysql # Statement 1
def delete():
mydb = mysql.connect(host="localhost", user="root",
passwd="root", database="emp")
mycursor = mydb.cursor()
__________________ # Statement 2
__________________ # Statement 3
print("Record deleted")Answer
python
import mysql.connector as mysql # Statement 1
mycursor.execute("DELETE FROM employee WHERE E_code='E101'") # Statement 2
mydb.commit() # Statement 3Explanation
Statement 1 imports the MySQL connector. Statement 2 executes the DELETE query through the cursor. Statement 3 commits the transaction so the deletion is permanent.