0% found this document useful (0 votes)
37 views6 pages

Interface of Python and Mysql - WKSHT

This document is a worksheet for Class XII students at Delhi Public School, Azaad Nagar, Kanpur, focusing on interfacing Python with MySQL. It includes multiple-choice questions, code completion tasks, and explanations regarding database connections, cursor usage, and data manipulation in MySQL using Python. The worksheet aims to assess students' understanding of Python-MySQL integration concepts and practical coding skills.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views6 pages

Interface of Python and Mysql - WKSHT

This document is a worksheet for Class XII students at Delhi Public School, Azaad Nagar, Kanpur, focusing on interfacing Python with MySQL. It includes multiple-choice questions, code completion tasks, and explanations regarding database connections, cursor usage, and data manipulation in MySQL using Python. The worksheet aims to assess students' understanding of Python-MySQL integration concepts and practical coding skills.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

DELHI PUBLIC SCHOOL,AZAAD NAGAR,KANPUR

Class-XII[2024-25]
WORKSHEET 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

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

3 Which function of connection is used to check whether connection to mysql is


successfully done or not?
import mysql.connector as msq
con = msq.connect( #Connection String ) # Assuming all parameter required as
passed if :
print(“Connected!”)
else:
print(“ Error! Not Connected”)
a. con.connected() b . con.isconnected()
c. con.is_connected() d. con.is_connect()

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?
or
A database ............... is a special control structure that facilitates the row by row processing
of records in the resultset.
a.ResultSet b. Cursor c. Container d. Table

5. Both A and R are true and R is the correct explanation for A

(b) Both A and R are true and R is not the correct explanation for A

(c) A is True but R is False

(d) A is false but R is True

Assertion. A database cursor receives all the records retrieved as per the query.

Reason. A resultset refers to the records in the database cursor and allows processing of
individual records in it.

Assertion. One by one the records can be fetched from the database directly through the
database connection.

Reason. The database query results into a set of records known as the resultset.
6.
The ________ function of mysql.connector is used for establishing
connection to a MYSQL database.
7
What is the difference in fetchall() and fetchone()?

8 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

9.
To reflect the changes made in the database permanently, you need to run <connection>.
............... method. Commit()

10. Which function is used to fetch n number of records from cursor?


a. fetch() b. fetchone() c. fetchmany() d. fetchall()

11.
Write code to connect to a MySQL database namely School and then fetch all those records
from table Student where grade is ' A' .

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 created query="select * from emp where
empno=_____".format(eno) mycursor.execute(query)
results = mycursor. print(results)

a.{ } and fetchone() b. fetchone() and { }


b.%s and fetchone() c. %eno and fetchone()
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 c con.save() function is missing
b. con.commit() function is missing d.commit() function is missing

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)
But query is giving error, What could be the possible reason?
a. fetchmany() should be used in place of fetchall()
b. fetchone() should be used in place of fetchone()
c. print(row) should be used in place of print(results)
d. loop and print function is wrong, for row in results: and print(row) should be
used

15 Guess the output

16. A resultset is extracted from the database using the cursor object (that has been already created)
by giving the following statement. Mydata=cursor.fetchone()
(a) How many records will be returned by fetchone() method?
(b) What will be the datatype of Mydata object after the given command is executed?

17. Kabir wants to write a program in Python to insert the following record in the table named
Student in MYSQL database, SCHOOL:
rno(Roll number )- integer
name(Name) - string
DOB (Date of birth) – Date
Fee – float
Note the following to establish connectivity between Python and MySQL:
Username - root
Password - tiger
Host - localhost
The values of fields rno, name, DOB and fee has to be accepted from the user. Help Kabir to
write the program in Python.

18. The code given below deletes the record from the table employee which contains the following
record structure:
Ecode-String
E name-String
Sal-Integer
City-String
Note the following to establish connectivity between Python and MySQL:
 Username is root
 Password is root
 The table exists in a MySQL database named emp
 The details (E_code,E_name,Sal City) are attributes of the table.
Write the following statements to complete the code:
Statement 1-to import the desired library
Statement2- to execute the command that deletes the record with E_code as 'E101'
Statement3 to delete the record permanently from the database.
import _____________ as mysql #statement 1
def delete():
mydb-mysql.connect (host="localhost", user="root", passwd="root", database="emp")
mycursor mydb.cursor()
__________________#Statement 2
___________________#Statement 3
Print ("Record deleted")

19. The code given below deletes the record from the table employee which contains the following
record structure:
Ecode-String
E name-String
Sal-Integer
City-String
Note the following to establish connectivity between Python and MySQL:
 Username is root
 Password is root
 The table exists in a MySQL database named emp
 The details (E_code,E_name,Sal City) are attributes of the table.
Write the following statements to complete the code:
Statement 1-to import the desired library
Statement2- to execute the query that fetches records of the employees coming from city
‘Delhi’.
Statement3 to read the complete data of the query (rows whose city is Delhi) into the object
named
details, from the table employee in the database.
import ____________ as mysql #Statement 1
def display():
mydb-mysql.connect(host="localhost", user="root", passwd="root", database="emp")
mycursor-mydb.cursort)
____________________ #Statement 2
details=_________________#Statement 3
for i in details:
print(i)

20. #set name =’Mani’ as per user’s choice e_code.


def update():
mydb-mysql. connect(host="localhost", user="root", passwd="root", database="emp")
mycursor-mydb.cursort)
____________________ #Statement 4
details=_________________#Statement 5
for i in details:
print(i)
r=input("Enter a E_code to be updated")
__________________#statement 4
_________________ #statement 5

21. The cursor.rowcount gives the count of records in the resultset.


22. The cursor.rowcount returns how many rows have been so far retrieved through fetch..()
methods from the cursor.
23. A DELETE or UPDATE or INSERT query requires commit() to reflect the changes in the
database.
24. Predict the output of the following code :

import mysql.connector
db = mysql.connector.connect(....)
cursor = db.cursor()
sql1 = "update category set name = '%s' WHERE ID = %s" % ('CSS',2)
cursor.execute(sql1)
db.commit()
print("Rows affected:", cursor.rowcount)
db.close()
25.

You might also like