I.
Multiple Choice Questions (1 mark each)
Q1. Which Python library is most commonly used to connect Python with MySQL
a) sqlite3
b) pymysql
c) mysql.connector
d) psycopg2
Q2. What does the following command do?
cursor = mydb.cursor()
a) Executes SQL queries
b) Creates a cursor object to interact with MySQL
c) Closes the connection
d) Creates a database
Q3. Which function is used to execute an SQL query in Python?
a) executequery()
b) exec()
c) execute()
d) runquery()
Q4. To permanently save changes made by INSERT in MySQL, which method must be used?
a) save()
b) commit()
c) submit()
d) close()
Q5. The output of fetchall() is:
a) A list of tuples
b) A single tuple
c) A dictionary
d) A single string
II. Identify the errors in the following code and rewrite it correctly:
import mysql.connector
mydb = mysql.connector.connect("localhost","root","1234","school")
crsr = mydb.cursor
crsr.execute(SELECT * FROM student)
data = crsr.fetchall
for i in data:
print(i)
correct code:
import mysql.connector
mydb = mysql.connector.connect( host="localhost", user="root", password="1234",
database="school")
crsr = mydb.cursor()
crsr.execute("SELECT * FROM student")
data = crsr.fetchall()
for i in data:
print(i)
mydb.close()
III. Case Study-Based Questions
Dataset (in MySQL table student)
roll name stream marks
1 Riya Science 78
2 Amit Commerce 56
3 Pooja Science 48
4 Mehul Arts 66
Q1.A school maintains the table student in its database.
Fill in the blanks in the following Python code to:
(a) Connect to the MySQL database school.
(b) Display all students having marks greater than 60.
import mysql.connector
# (a) Establish connection
mydb = mysql.connector.connect(host="localhost", user="root", password="1234",
database= _____________ # Blank 1)
cursor = _____________ # Blank 2
# (b) Execute query
cursor.execute("SELECT * FROM student WHERE marks > ________") # Blank 3
data = _____________ # Blank 4
for row in data:
print(row)
mydb. _____________ # Blank 5
Ans:
1. "school"
2. mydb.cursor()
3. 60
4. cursor.fetchall()
5. close()
Q2.Complete the Python code to insert a new record (5, "Kiran", "Science", 82) into the
student table:
import mysql.connector
mydb = mysql.connector.connect(host="localhost", user="root", password="1234",
database="school")
cursor = mydb. _____________ # Blank 1
sql = "INSERT INTO student (roll, name, stream, marks) VALUES ( %s , %s , %s , %s )"
val = ( ________, "Kiran", "Science", ________ ) # Blank 2 & 3
cursor. _____________ (sql, val) # Blank 4
mydb. _____________ # Blank 5
print("Record inserted")
mydb.close()
Ans:
1. mydb.cursor()
2. 5
3. 82
4. execute
5. commit()
Q3.Fill in the blanks to update marks of all Science students by adding 5 marks.
import mysql.connector
mydb = mysql.connector.connect(host="localhost",user="root",password="1234",
database="school")
cursor = mydb.cursor()
sql = " _____________ student SET marks = marks + 5 WHERE stream = 'Science'" # Blank 1
cursor. _____________ (sql) # Blank 2
mydb. _____________ # Blank 3
mydb.close()
Ans:
1. UPDATE
2. execute
3. commit()
Q4.Write the missing parts of the code to display the first two rows from the student table.
import mysql.connector
mydb = mysql.connector.connect(host="localhost", user="root", password="1234",
database="school")
cursor = mydb.cursor()
cursor.execute("SELECT * FROM student")
data = cursor. _____________ (2) # Blank 1
for row in data:
_____________ # Blank 2
mydb.close()
Ans:
1. fetchmany
2. print(row)