Worksheets › Class 12 › Interface Python with MySQL
Class 12 · Computer Science worksheetInterface Python with MySQL
CBSE Computer Science. connect(), cursor(), execute(), commit(), fetchone(), fetchall() and rowcount, %s placeholders and format(), traced on a table. Every sheet has 20 questions in three sections and an answer key, and the questions change every time.
Sample questions
Real questions from this chapter, as the app sets them. Tap Answer to see the answer and working.
Section A · multiple choice
- 1.
Which function opens a connection from Python to a MySQL database?
- execute()
- open()
- cursor()
- connect()
Answer
(d) connect()
- 2.
Table: STUDENT RollNo | Name | Sec | City | Marks 101 | Dev | C | Delhi | 47 102 | Ananya | A | Delhi | 58 103 | Zoya | B | Delhi | 92 104 | Isha | B | Delhi | 46 105 | Vikram | C | Indore | 68 106 | Rahul | C | Pune | 40 107 | Priya | C | Delhi | 96 108 | Kabir | A | Indore | 83 What does this program print? (Output lines are separated by commas.) import mysql.connector as m con = m.connect(host='localhost', user='root', password='****', database='school') cur = con.cursor() cur.execute("SELECT Name, Marks FROM STUDENT WHERE Marks BETWEEN 58 AND 83 ORDER BY Marks") data = cur.fetchall() print(len(data), cur.rowcount)
- 3 3
- 3 1
- 1 3
- 3 0
Answer
(a) 3 3
fetchone() gives the next row as a tuple; fetchall() gives all remaining rows as a list of tuples
- 3.
Table: STUDENT RollNo | Name | Sec | City | Marks 101 | Zoya | C | Kochi | 65 102 | Dev | A | Kochi | 73 103 | Ananya | A | Indore | 56 104 | Vikram | A | Kochi | 94 105 | Rohan | C | Kochi | 66 106 | Kabir | A | Kochi | 76 After cur.execute("UPDATE STUDENT SET Marks = Marks + 4 WHERE Marks < 66") and con.commit(), what is cur.rowcount?
- 1
- 6
- 2
- 3
Answer
(c) 2
rows where Marks < 66
Section B · short answer
- 4.
What is the use of a cursor in Python–MySQL connectivity?
Answer
It is the object that sends SQL statements to the database with execute() and holds the result rows to be fetched.
- 5.
Table: STUDENT RollNo | Name | Sec | City | Marks 201 | Rahul | B | Jaipur | 80 202 | Arjun | A | Kochi | 69 203 | Kabir | B | Jaipur | 75 204 | Ananya | B | Jaipur | 91 205 | Priya | B | Kochi | 70 206 | Meera | C | Shimla | 90 207 | Rohan | A | Kochi | 83 What does this program print? import mysql.connector as m con = m.connect(host='localhost', user='root', password='****', database='school') cur = con.cursor() cur.execute("SELECT Name, Marks FROM STUDENT WHERE Marks BETWEEN 70 AND 75 ORDER BY Marks DESC") data = cur.fetchall() print(len(data), cur.rowcount)
Answer
2 2
fetchone() returns a tuple; fetchall() a list of tuples
Section C · long answer
- 6.
Explain the steps of connecting Python with MySQL to display records, with the code for each step.
Answer
1. import mysql.connector as m; 2. con = m.connect(host='localhost', user='root', password='…', database='school'); 3. cur = con.cursor(); 4. cur.execute('SELECT * FROM STUDENT'); 5. rows = cur.fetchall(); for r in rows: print(r); 6. con.close().
Fresh sheets for your child
In the app, every Interface Python with MySQL sheet is new: the app remembers the questions your child has already had and leaves them out. Print the sheet as a PDF, or use notebook mode and write the answers in a notebook.
Coming soon for Android and iPhone.