Test Index

Interface of Python with SQL Database Get it on Google PlayDownload on the App Store CBSE Board Videos

© examsnet.com
Question : 10 of 10
Marks: +1, -0
Consider the following SQL table MEMBER in a SQL Database CLUB :
Table : MEMBER
M_IDNAMEACTIVITY
M1001AminaGYM
M1002PratikGYM
M1003SimonSWIMMING
M1004RakeshGYM
M1005AvneetSWIMMING
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 = "#")
[CBSE 2022 Code 91]
Solution:  
Output:
M1002#Pratik
M1004#Rakesh
Explanation:
The query selects the rows with ACTIVITY 'GYM', in table order: (M1001, Amina), (M1002, Pratik), (M1004, Rakesh).
1. The first fetchone() (before the loop) reads row 1 (M1001, Amina), but it is not printed.
2. Loop, i = 0: fetchone() reads the next row (M1002, Pratik) and prints M1002#Pratik.
3. Loop, i = 1: fetchone() reads (M1004, Rakesh) and prints M1004#Rakesh.
sep = "#" places # between the two values, and each print() ends with a new line.
Key point: Each call of fetchone() moves the cursor one row ahead, so the row fetched before the loop is skipped in the output.
© examsnet.com
Advertisements
Go to Question: