Analyze the following Python program that demonstrates connectivity between…

2026

Analyze the following Python program that demonstrates connectivity between Python and a MySQL database. The program is intended to insert a new record into a table named MYCONTACTS in the PHONEBOOK database. Some object and method names are missing from the code. Assume that an appropriate module for connectivity has been used and that it is functioning properly.

import mysql.connector as SQL           # Line 01

MyDB = SQL._____ (host='localhost', \
    user='root', password='tiger', \
    database='PHONEBOOK')              # Line 02

MyCursor = MyDB._____ ()               # Line 03

print('Enter the details of a row: ')  # Line 04
Name = input('Enter Name: ')           # Line 05
Phone = input('Enter Phone Number: ')  # Line 06

Data = (Name, Phone)                  # Line 07

Command = "INSERT INTO MYCONTACTS VALUES(%s,%s);"  # Line 08

MyCursor._____ (Command, Data)        # Line 09

_____.commit()                        # Line 10

(A) Write the name of the method/function in Line 02 required to establish database connectivity.

(B) Write the name of the method/function in Line 03 required to create the cursor object MyCursor.

(C) Write the name of the method/function in Line 09 required to execute the MySQL query successfully.

(D) Write the name of the object in Line 10 used to commit the changes (INSERT operation) successfully.

Show answer & explanation

(A) Method in Line 02 (Database Connectivity):

  • The function used to establish connection with MySQL database is:
    connect

  • Complete statement: SQL.connect(...)

(B) Method in Line 03 (Create Cursor Object):

  • The method used to create a cursor object is:
    cursor

  • Complete statement: MyDB.cursor()

(C) Method in Line 09 (Execute Query):

  • The method used to execute SQL queries is:
    execute

  • Complete statement: MyCursor.execute(Command, Data)

(D) Object in Line 10 (Commit Changes):

  • The object used to commit changes to the database is:
    MyDB

  • Complete statement: MyDB.commit()

Final Answers:

  • (A) connect

  • (B) cursor

  • (C) execute

  • (D) MyDB

Explore the full course: Hpsc Pgt Computer Science