English

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 - Computer Science (Python)

Advertisements
Advertisements

Question

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
Code Writing
Advertisements

Solution

import mysql.connector as sql

1. Establish connectivity with provided credentials
conn = sql.connect(
    host = 'localhost',
    user = 'admin',
    password = 'root',
    database = 'SCHOOL'
)

2. Create a cursor object
cursor = conn.cursor()

3. Define the query to find students with fees less than 5000
query = "SELECT * FROM Account WHERE Fees < 5000"

4. Execute the query
cursor.execute(query)

5. Display the records
print("Records of students with fees less than 5000:")
for rec in cursor:
    print(rec)

6. Close the connection
conn.close()
shaalaa.com
  Is there an error in this question or solution?
2025-2026 (March) Set 4
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×