Test Index

CBSE Class 12 Computer Science 2026 Paper Set 4 Get it on Google PlayDownload on the App Store

© examsnet.com
Question : 35 of 37
Marks: +1, -0
Peter has created a table named Account in MySQL database, SCHOOL, having following structure :
• Stud_id – integer
• Sname – string
• Class – string
• Fees – float
Help him in writing a Python program to display records of those students whose fees is less than 5000.
Note the following to establish connectivity between Python and MySQL :
• Username – admin
• Password – root
• Host – localhost
Solution:  
Concept: Steps of Python–MySQL connectivity: import the connector, connect with host, user, password and database, create a cursor, execute the SELECT query, fetch the rows, display them and close the connection.
import mysql.connector as mycon
con = mycon.connect(host="localhost", user="admin",
password="root", database="SCHOOL")
cur = con.cursor()
cur.execute("SELECT * FROM Account WHERE Fees < 5000")
data = cur.fetchall()
for row in data:
print(row)
con.close()
Explanation:
• connect() opens the connection to the SCHOOL database with the given user name and password.
• execute() runs the query; the WHERE clause keeps only students with Fees below 5000.
• fetchall() returns all matching rows as a list of tuples, and the loop prints each record.
• import pymysql with pymysql.connect(...) works the same way.
© examsnet.com
Advertisements
Go to Question: