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 designed to display all the rows (records) from a table named MYCONTACTS in the PHONEBOOK database, along with the total number of rows present in the table. An appropriate connectivity module has been correctly installed and is functioning properly. Some object and method/function names are missing from the code.

import mysql.connector as SQL        # Line 01

MyDB = SQL.connect(host='localhost', user='root',
                   password='bharat',
                   database='PHONEBOOK')            # Line 02

MyCursor = __A1__.__A2__             # Line 03

__B1__.__B2__("SELECT * FROM MYCONTACTS;")   # Line 04

print('Name\t Phone')                # Line 05

AllRow = __C1__.__C2__               # Line 06

for aRow in AllRow:                  # Line 07
    print(aRow[0], '\t\t', aRow[1])  # Line 08

print('\nTotal rows:', __D1__.__D2__)  # Line 09

Questions:

(A) Write the name of the object (A1) and method/function (A2) required to create the cursor object in Line 03.

(B) Write the name of the object (B1) and method/function (B2) required to execute the MySQL query in Line 04.

(C) Write the name of the object (C1) and method/function (C2) required to fetch all rows/records into the identifier AllRow in Line 06.

(D) Write the name of the object (D1) and method/function (D2) required to obtain the total number of rows/records in Line 09.

Show answer & explanation

(A) Cursor Creation:
A1 = MyDB
A2 = cursor()
Answer: MyDB.cursor()
Reason: The cursor() method is used to create a cursor object for executing SQL queries.

(B) Executing SQL Query:
B1 = MyCursor
B2 = execute
Answer: MyCursor.execute()
Reason: The execute() method is used to run SQL queries on the database.

(C) Fetching All Records:
C1 = MyCursor
C2 = fetchall()
Answer: MyCursor.fetchall()
Reason: fetchall() retrieves all rows from the executed query result.

(D) Counting Total Rows:
D1 = MyCursor
D2 = rowcount
Answer: MyCursor.rowcount
Reason: rowcount returns the number of rows retrieved by the query.

Explore the full course: Hpsc Pgt Computer Science