Test Index
Interface of Python with SQL Database
© examsnet.com
Question : 10 of 10
Marks:
+1,
-0
Consider the following SQL table MEMBER in a SQL Database CLUB :Table : MEMBER
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 :
M_ID | NAME | ACTIVITY |
|---|---|---|
M1001 | Amina | GYM |
M1002 | Pratik | GYM |
M1003 | Simon | SWIMMING |
M1004 | Rakesh | GYM |
M1005 | Avneet | SWIMMING |
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#PratikM1004#RakeshExplanation: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
Go to Question: