CBSE · 083Class XII · 2023Section D3 marks

Question 32(2or)

30 March 2023 · Computer Science (083)

The code given below reads the records from the table employee and displays only those whose city is 'Delhi'. The table has the structure :

text
E_code  - String
E_name  - String
Sal     - Integer
City    - String

Note: Username is root, Password is root, MySQL database is named emp.

Write the following statements to complete the code : - Statement 1 — to import the desired library. - Statement 2 — to execute the query that fetches records of employees from city 'Delhi'. - Statement 3 — to read the complete data of the query into the object named details.

python
import __________ as mysql        # Statement 1

def display():
    mydb = mysql.connect(host="localhost", user="root",
                         passwd="root", database="emp")
    mycursor = mydb.cursor()
    __________________            # Statement 2
    details = ________            # Statement 3
    for i in details:
        print(i)

Answer

python
import mysql.connector as mysql                                          # Statement 1

mycursor.execute("SELECT * FROM employee WHERE City='Delhi'")            # Statement 2
details = mycursor.fetchall()                                            # Statement 3

Explanation

Statement 1 imports the MySQL connector. Statement 2 runs a SELECT filtered by City='Delhi'. Statement 3 retrieves all matching rows from the cursor via fetchall().