Computer Science - Class 12
Solutions to CBSE Sample Paper - Computer Science Class 12

(b) The code given below reads the following record from the table named student and displays only those records who have marks greater than 75:

RollNo – integer

Name – string

Clas – integer

Marks – integer

Note the following to establish connectivity between Python and

MYSQL:

  • Username is root

  • Password is tiger

  • The table exists in a MYSQL database named school.

Write the following missing statements to complete the code:

Statement 1 – to form the cursor object

Statement 2 – to execute the query that extracts records of those students whose marks are greater than 75.

Statement 3- to read the complete result of the query (records whose marks are greater than 75) into the object named data, from the table student in the database.

import mysql.connector as mysql

def sql_data():

 

con1=mysql.connect(host="localhost",user="root",

password="tiger", database="school")

mycursor=_______________ #Statement 1

print("Students with marks greater than 75 are :

")

_________________________ #Statement 2

data=__________________ #Statement 3

for i in data:

print(i)

print() 

 

Answer:

import mysql.connector as mysql

def sql_data ():

    con1=mysql.connect(host= "localhost" ,user= "root" ,password= "tiger" , database="school")

    mycursor=con1.cursor() #Statement 1

    print( "Students with marks greater than 75 are : " )

    mycursor.execute( "select * from student where Marks>75" ) #Statement 2

    data=mycursor.fetchall() #Statement 3

    for i in data:

        print(i)

        print()

 

Explanation:

Statement 1: To form the cursor object, the cursor( ) method is used.

          Syntax: cursor_object=connection_object.cursor( )

Statement 2: To execute a MySQL command, the execute( ) method is used.

          Syntax: cursor_object.execute(“MySQL query”/query_string)

Statement 3: To read the complete result of the query, the fetchall( ) method is used.                    

                       Syntax: cursor_object.fetchall( )

Ask a doubt
Davneet Singh's photo - Co-founder, Teachoo

Made by

Davneet Singh

Davneet Singh has done his B.Tech from Indian Institute of Technology, Kanpur. He has been teaching from the past 14 years. He provides courses for Maths, Science, Social Science, Physics, Chemistry, Computer Science at Teachoo.