In this section we will see how to create a table, inserting a value and deleting them from the table created.
First we will see the code and will go with the explanation.
Code
Table Creation
import mysql.connector
cnx = mysql.connector.connect(user='root', password='Zoku&1236',
host='localhost', database='mermaid')
cnx_cursor = cnx.cursor()
cnx_cursor.execute("CREATE TABLE student (id INT, name VARCHAR(255))")
cnx_cursor.execute("SHOW TABLES")
for table in cnx_cursor:
print(table)
cnx.close()
Inserting value
import mysql.connector
cnx = mysql.connector.connect(user='root', password='Zoku&1236',
host='localhost', database='mermaid')
cnx_cursor = cnx.cursor()
sql_query_1 = "INSERT INTO student (id, name) VALUES (01, 'John')"
sql_query_2 = "SELECT * FROM student"
cnx_cursor.execute(sql_query_1)
cnx_cursor.execute(sql_query_2)
for table in cnx_cursor:
print(table)
cnx.close()
Deleting value
import mysql.connector
cnx = mysql.connector.connect(user='root', password='Zoku&1236',
host='localhost', database='mermaid')
cnx_cursor = cnx.cursor()
sql_query_1 = "INSERT INTO student (id, name) VALUES (01, 'John')"
sql_query_2 = "SELECT * FROM student"
cnx_cursor.execute(sql_query_1)
cnx_cursor.execute(sql_query_2)
for table in cnx_cursor:
print(table)
cnx.close()
Explanation
d