WORKSHEET of Interfacing ( (July)
WORKSHEET of Interfacing ( (July)
WORKSHEET - 4 Grade-12
INTERFACE PYTHON WITH MYSQL
1 Identify the name of connector to establish bridge between Python and MySQL
a. mysql.connection
b. connector
c. mysql.connect
d. mysql.connector
Ans
2 In the following connection string: Identify the elements:
connect( <<1>> = 127.0.0.1, <<2>> =‟ root‟, <<3>> = „admin‟)
a. <<1>> = User, <<2>> = password, <<3> = host
b. <<1>> = host, <<2>> = user, <<3> = password
c. <<1>> = host, <<2>> = password, <<3> = user
d. <<1>> = IP, <<2>> = user, <<3> = password
Ans
a. con.connected()
b. con.isconnected()
c. con.is_connected()
d. con.is_connect()
Ans
4 Which of the following component act as a container to hold all the data returned from the
query and from there we can fetch data one at a time?
a. ResultSet
b. Cursor
c. Container
d. Table
Ans
5 Identify the correct statement to create cursor:
import mysql.connector as msq
con = msq.connect( #Connection String ) # Assuming all parameter required as passed
mycursor =
a. con.cursor()
b. con.create_cursor()
c. con.open_cursor()
d. con.get_cursor()
Ans
6 What is the difference in fetchall() and fetchone()?
Ans
7 Which attribute of of cursor is used to get number of records stored in cursor (Assuming cursor
name is mycursor)?
a. mycursor.count
b. mycursor.row_count
c. mycursor.records
d. mycursor.rowcount
Ans
8 Which of the Symbols are used for passing parameterized query for execution to cursor?
a. %
b. {}
c. $
d. Both a and b
Ans
9 Which function is used to fetch n number of records from cursor?
a. fetch()
b. fetchone()
c. fetchmany()
d. fetchall()
Ans
10 Which cursor function is used to send query to connection?
a. query()
b. execute()
c. run()
d. send()
Ans
11 Consider the information stored in the table : EMP
EMPNO ENAME DEPT SALARY
1 ALEX MUSIC 60000
2 PETER ART 67000
3 JOHNY WE 55000
4 RAMBO P&HE 48000
Following python code is written to access the records of table: EMP, What will be the output of
following code:
# Assume All basic setup related to connection and cursor creation is already done query="select *
from emp"
mycursor.execute(query) results
= mycursor.fetchone()results =
mycursor.fetchone()results =
mycursor.fetchone()d =
int(results[3])
print(d*3)
a. P&HEP&HEP&HE
b. 144000
c. WEWEWE
d. 165000
Ans
12 Consider the following Python code is written to access the record of CODE passed to function:
Complete the missing statements:
def Search(eno):
#Assume basic setup import, connection and cursor is
createdquery="select * from emp where empno=
".format(eno)
mycursor.execute(query)
results = mycursor. print(results)
a. { } and fetchone()
b. fetchone() and { }
c. %s and fetchone()
d. %eno and fetchone()
Ans
13 Consider the following Python code for updating the records:
def Update(eno):
#Assume basic setup import, connection(con) and cursor(mycursor) is created
query="update emp set salary=90000 where empno=” + str(eno)
mycursor.execute(query)
Code is running but the record in actual database is not updating, what could be the possible
reason?
a. save() function is missing
b. con.save() function is missing
c. con.commit() function is missing
d. commit() function is missing
Ans
14 Consider the following python code to display all records from table: EMP
def showAll():
#Assume basic setup import, connection(con) and cursor(mycursor) is created
query="select * from emp"
mycursor.execute(query)
results =
mycursor.fetchall() for
results in row:
print(results)