fork download
  1. import mysql.connector
  2. mycon = mysql.connector.connect(
  3. host='localhost',
  4. user='root',
  5. password='password',
  6. database='School')
  7. if mycon.is_connected():
  8. print('Database connected')
  9. cursor = mycon.cursor()
  10. sql =''' CREATE TABLE IF NOT EXISTS STUDENT (
  11. ID INT PRIMARY KEY,
  12. Name CHAR(10),
  13. Stream CHAR(10),
  14. AvgMark FLOAT,
  15. Grade CHAR(2),
  16. Class CHAR(5))'''
  17. cursor.execute(sql)
  18. sql = ''' INSERT INTO STUDENT (ID, Name, Stream, AvgMark, Grade, Class)VALUES
  19. (1, 'Karan', 'Medical', 78.5, 'B', '128'),
  20. (2, 'Divakar', 'Commerce', 89.2, 'A', '11C'),
  21. (3, 'Divya', 'Commerce', 68.6, 'C', '12C'),
  22. (4, 'Arun', 'Computers', 89.9, 'A', '12A'),
  23. (5, 'Sabina', 'Medical', 83.3, 'B', '128') '''
  24. cursor.execute(sql)
  25. mycon.commit()
  26. cursor.execute('SELECT * FROM STUDENT')
  27. data = cursor.fetchall()
  28. for rec in data:
  29. print(rec)
  30. mycon.close()
Success #stdin #stdout 0.03s 25876KB
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()
sql =''' CREATE TABLE IF NOT EXISTS STUDENT (
    ID INT PRIMARY KEY,
    Name CHAR(10),
    Stream CHAR(10),
    AvgMark FLOAT,
    Grade CHAR(2), 
    Class CHAR(5))'''
cursor.execute(sql)
sql = ''' INSERT INTO STUDENT (ID, Name, Stream, AvgMark, Grade, Class)VALUES
     (1, 'Karan', 'Medical', 78.5, 'B', '128'),
     (2, 'Divakar', 'Commerce', 89.2, 'A', '11C'),
     (3, 'Divya', 'Commerce', 68.6, 'C', '12C'),
     (4, 'Arun', 'Computers', 89.9, 'A', '12A'),
     (5, 'Sabina', 'Medical', 83.3, 'B', '128') '''
cursor.execute(sql)
mycon.commit()
cursor.execute('SELECT * FROM STUDENT')
data = cursor.fetchall()
for rec in data:
   print(rec)
mycon.close()