Grade XII – Computer Science
Python–MySQL Connectivity – Revision Worksheet
Part A – Theory (1 mark each)
Q1. Write the name of the Python module used to connect with MySQL.
Q2. Arrange the following steps of Python–MySQL connectivity in correct order:
a. Execute SQL statements
b. Close connection
c. Import module
d. Create cursor object
e. Establish connection
Q3. Name the method used to fetch:
(a) One row from a query result
(b) All rows from a query result
Q4. What is the purpose of commit() method in MySQL–Python connectivity?
Part B – Output Prediction (2 marks each)
Q5.
import mysql.connector
con = mysql.connector.connect(host="localhost", user="root", passwd="1234",
database="school")
cur = con.cursor()
cur.execute("SELECT Name FROM student WHERE Marks > 80")
rows = cur.fetchall()
for r in rows:
print(r[0])
If table student contains:
RollNo Name Marks
1 Ajay 78
2 Sneha 84
3 Ritu 90
Output:
Q6.
import mysql.connector
con = mysql.connector.connect(host="localhost", user="root", passwd="1234",
database="company")
cur = con.cursor()
cur.execute("SELECT * FROM emp WHERE Salary BETWEEN 30000 AND 40000")
print(cur.fetchone())
If table emp contains:
EID Name Salary
101 Rahul 35000
102 Meena 42000
103 Arjun 39000
Output:
Part C – Error Finding & Correction (2 marks each)
Q7.
import mysql.connector
mycon = mysql.connector.connect("localhost", "root", "1234", "school")
cur = mycon.Cursor()
Q8.
cur.execute("SELECT Name Salary FROM emp")
Part D – Program Writing (3 marks each)
Q9. Write a Python program to insert a record (105, 'Neha', 47000) into table emp(EID
INT, Name VARCHAR(20), Salary INT) in database company.
Q10. Write a Python program to display the Name and Salary of all employees whose salary
is more than 50000 from table emp.
Q11. Write a Python program to update the salary of employee with EID = 103 to 55000 in
table emp.
Part E – Integrated Board-style Questions (4–5 marks each)
Q12. A database school has a table student as follows:
RollNo Name Marks
1 Amit 85
2 Priya 78
3 Rohan 92
4 Neha 88
Write a Python program to:
1. Connect to MySQL
2. Display names of students scoring between 80 and 90
Q13. The table book in database library has:
BID Title Author
1 The Alchemist Paulo Coelho
2 Wings of Fire A P J Abdul Kalam
3 Python Magic Rahul Sharma
Write a Python program to:
Insert a new record (4, 'Think Python', 'Allen Downey')
Display all records from book table