fork download
  1. import mysql.connector
  2.  
  3. mycon= mysql.connector.connect(
  4. host='localhost',
  5. user='root',
  6. password='password',
  7. database='School')
  8.  
  9. # Display all the records of students who are in the Medical stream:
  10.  
  11. if mycon.is_connected():
  12. print('Database connected')
  13. cursor = mycon.cursor()
  14. sql ="SELECT * FROM STUDENT WHERE Stream='Medical' "
  15. cursor.execute(sql)
  16. records = cursor.fetchall()
  17. print("Answer for a):")
  18. for rec in records:
  19. print(rec)
  20.  
  21. # Change the Class of Divakar to 12C:
  22.  
  23. sql ="UPDATE STUDENT SET Class='12C' WHERE Name='Divakar'"
  24. cursor.execute(sql)
  25. mycon.commit()
  26. print("Record Updated")
  27.  
  28. # Display the maximum mark of students in class 12:
  29.  
  30. sql ="SELECT MAX(AvgMark) FROM STUDENT WHERE Class='12'"
  31. cursor.execute(sql)
  32. max_mark = cursor.fetchone()[0]
  33. print("Answer for c):")
  34. print("The maximum mark in class 12 is:{max_mark}")
  35.  
  36. mycon.close()
  37.  
Success #stdin #stdout 0.02s 26020KB
stdin
Standard input is empty
stdout
import  mysql.connector

mycon= mysql.connector.connect(
            host='localhost',
            user='root',
            password='password',
            database='School')
            
# Display all the records of students who are in the Medical stream:

if  mycon.is_connected():
      print('Database connected')
cursor = mycon.cursor()
sql ="SELECT * FROM STUDENT WHERE Stream='Medical' "
cursor.execute(sql)
records = cursor.fetchall()
print("Answer for a):")
for rec in records:
print(rec)

# Change the Class of Divakar to 12C:

sql ="UPDATE STUDENT SET Class='12C' WHERE Name='Divakar'"
cursor.execute(sql)
mycon.commit()
print("Record Updated")

# Display the maximum mark of students in class 12:

sql ="SELECT MAX(AvgMark) FROM STUDENT WHERE Class='12'"
cursor.execute(sql)
max_mark = cursor.fetchone()[0]
print("Answer for c):")
print("The maximum mark in class 12 is:{max_mark}")

mycon.close()