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. if mycon.is_connected():
  9. print('Database connected')
  10.  
  11. cursor = mycon.cursor()
  12. sql='''
  13. CREATE TABLE IF NOT EXISTS STUDENT (
  14. ID INT PRIMARY KEY,
  15. Name CHAR(10),
  16. Stream CHAR(10),
  17. AvgMark FLOAT,
  18. Grade CHAR(2),
  19. Class CHAR(5)) '''
  20.  
  21.  
  22. cursor.execute(sql)
  23. sql='''
  24. INSERT INTO STUDENT (ID, Name, Stream, AvgMark, Grade, Class) VALUES
  25. (1, 'Karan', 'Medical', 78.5, 'B', '128'),
  26. (2, 'Divakar', 'Commerce', 89.2, 'A', '11C'),
  27. (3, 'Divya', 'Commerce', 68.6, 'C', '12C'),
  28. (4, 'Arun', 'Computers', 89.9, 'A', '12A'),
  29. (5, 'Sabina', 'Medical', 83.3, 'B', '128') '''
  30.  
  31. cursor.execute(sql)
  32. mycon.commit()
  33. cursor.execute('SELECT * FROM STUDENT')
  34. data = cursor.fetchall()
  35. for rec in data:
  36. print(rec)
  37.  
  38. mycon.close()
  39.  
Success #stdin #stdout 0.02s 26144KB
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()