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. if mycon.is_connected():
  10. print('Database connected')
  11.  
  12. cursor = mycon.cursor()
  13.  
  14. # Count the number of records in each stream and display it:
  15.  
  16. sql ='SELECT Stream, COUNT(*) FROM STUDENT GROUP BY
  17. Stream'
  18. cursor.execute(sql)
  19. data = cursor.fetchall()
  20. print("Answer for a):")
  21. for rec in data:
  22. print(rec)
  23.  
  24.  
  25.  
  26. # Display the number of students who have secured 'A' Grade:
  27.  
  28. sql ="SELECT COUNT(*) FROM STUDENT WHERE Grade='A'"
  29. cursor.execute(sql)
  30. count = cursor.fetchone()[0]
  31. print("The number of students with 'A' grade is {count}")
  32.  
  33. # Add a new column called Rank in the table:
  34.  
  35. sql ='ALTER TABLE STUDENT ADD Rank CHAR(3)'
  36. cursor.execute(sql)
  37. mycon.commit()
  38. print('Field Added')
  39.  
  40. mycon.close()
  41.  
Success #stdin #stdout 0.02s 25896KB
stdin
Standard input is empty
stdout
import mysql.connector

mycon=mysql.connector.connect(
         host='localhost',
         user='root',
         password='password', 
         database='School')

if mycon.is_connected(): 
    print('Database connected')

cursor = mycon.cursor()

# Count the number of records in each stream and display it:

sql ='SELECT Stream, COUNT(*) FROM STUDENT GROUP BY      
          Stream'
cursor.execute(sql)
data = cursor.fetchall()
print("Answer for a):")
for rec in data:
      print(rec)



# Display the number of students who have secured 'A' Grade:

sql ="SELECT COUNT(*) FROM STUDENT WHERE Grade='A'"
cursor.execute(sql)
count = cursor.fetchone()[0]
print("The number of students with 'A' grade is {count}")

# Add a new column called Rank in the table:

sql ='ALTER TABLE STUDENT ADD Rank CHAR(3)'
cursor.execute(sql)
mycon.commit()
print('Field Added')

mycon.close()