import mysql.connector
connection = mysql.connector.connect(host="localhost",user="root",password="root") #connection object to connect python with mysql
print(connection)
if connection.is_connected(): #print connection object memory address
print("Python mysql connected") #check connection is established or not
cursor = connection.cursor() #create cursor to activate pointer to execute commands
cursor.execute("CREATE DATABASE PYMYSQL;") #run execute() function to execute queries
cursor.execute("USE PYMYSQL;")
cursor.execute("""CREATE TABLE STUDENTS
(
S_NAME CHAR(50),
AGE INT
);""")
cursor.execute("INSERT INTO STUDENTS VALUES('KARAN',17);")
cursor.execute("""INSERT INTO STUDENTS VALUES
("MAHMOOD",16),
("NITHIN",17);""")
connection.commit() #commit function accept database and table creation and insert records into it.
connection.close() #To close the connection
else:
print("Python mysql not connected")