Q.Consider the following SQL table MEMBER in a SQL Database CLUB: Table: MEMBER M_ID NAME ACTIVITY M1001 Amina GYM M1002 Pratik GYM M1003 Simon SWIMMING M1004 Rakesh GYM M1005 Avneet SWIMMING Assume that the required library for establishing the connection between Python and MYSQL is already imported in the given Python code. Also assume that DB is the name of the database connection for table MEMBER stored in the database CLUB. Predict the output of the following code: MYCUR = DB.cursor() MYCUR.execute("USE CLUB") MYCUR.execute("SELECT * FROM MEMBER WHERE ACTIVITY= 'GYM' ") R=MYCUR.fetchone() for i in range(2): R=MYCUR.fetchone() print(R[0], R[1], sep = "#")
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →The code fetches rows from the MEMBER table where ACTIVITY is 'GYM'. Due to the sequence of fetchone() calls, the first row is fetched before the loop, and the next two rows are fetched inside the loop. The print statement then outputs the M_ID and NAME of the third fetched row, separated by '#'. The output is M1004#Rakesh.
When working with databases in Python, understanding how a cursor interacts with a result set is crucial. The fetchone() method is stateful; it retrieves one row at a time and advances an internal pointer to the next row in the result set. This means subsequent calls to fetchone() will return different rows until all rows are exhausted.
Let's break down the process:
-
Database Table and Query Result:
First, let's visualize the
MEMBERtable and the result of the SQL query.Table: MEMBER
M_ID NAME ACTIVITY M1001 Amina GYM M1002 Pratik GYM M1003 Simon SWIMMING M1004 Rakesh GYM M1005 Avneet SWIMMING The SQL query
SELECT * FROM MEMBER WHERE ACTIVITY = 'GYM'will filter the table to include only members whoseACTIVITYis 'GYM'. The result set, in the order they would typically be returned (assuming no specificORDER BYclause, often by primary key or insertion order), would be:Result Set for
ACTIVITY = 'GYM'M_ID NAME ACTIVITY M1001 Amina GYM M1002 Pratik GYM M1004 Rakesh GYM -
Code Execution Trace:
-
MYCUR = DB.cursor(): A cursor object,MYCUR, is created. This object is used to execute SQL commands and fetch results. -
MYCUR.execute("USE CLUB"): This SQL command sets the active database context toCLUB. This is a preparatory step, ensuring subsequent queries operate on the correct database. -
MYCUR.execute("SELECT * FROM MEMBER WHERE ACTIVITY= 'GYM' "): The SQL query is executed. The database processes this query and prepares the result set (the three rows listed above). The cursor's internal pointer is now positioned before the first row of this result set. -
R = MYCUR.fetchone():This is the first call to
fetchone(). It retrieves the first row from the result set.Rnow holds the tuple('M1001', 'Amina', 'GYM').The cursor's internal pointer advances to the next row.
-
for i in range(2)::This loop will iterate twice, for
i = 0andi = 1.- Iteration 1 (i = 0):
R = MYCUR.fetchone(): This is the second call tofetchone(). It retrieves the next available row from the result set.Rnow holds the tuple('M1002', 'Pratik', 'GYM'). …
- Iteration 1 (i = 0):
-
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.