1import mysql.connector
2
3# Connect to MySql
4db = mysql.connector.connect(
5
6 host="localhost",
7 user="root",
8 passwd="root",
9 database="testdatabase"
10
11 )
12
13mycursor = db.cursor()
14
15# Make sure you have created a database before:
16
17# Creating a table named Test
18mycursor.execute("CREATE TABLE Test (name VARCHAR(50) NOT NULL, gender ENUM('M', 'F') NOT NULL, id int PRIMARY KEY NOT NULL AUTO_INCREMENT)")
19
20# Inserting the element in the table
21mycursor.execute("INSERT INTO Test (name, created, gender) VALUES (%s, %s, %s)", ("Peter", datetime.now(), 'M'))
22
23# Apply the Data to the server
24db.commit()
25
26# Iterating over all rows in the Database
27for x in mycursor:
28 print(x)