MYSQL DATABASE WITH PYTHON

Installing requirements:

sudo apt update
sudo apt install python3-pip
sudo pip3 install mysql-connector

Test importing MySQL to Python:

python3

>> import mysql

If no error, it is working.

The basic syntax to interact with MySQL.

import mysql.connector

db = mysql.connector.connect(host="localhost",user="user",passwd="password",database="dbName")
mycursor = db.cursor()
mycursor.execute("SHOW TABLES")

for line in mycursor:
  print(line)

Inserting data:

mycursor.execute("INSERT INTO %s (attrib1, attrib2) VALUES (%s, %s)", ("tableName","data1","data2"))
db.commit()

mycursor.execute("SELECT %s FROM %s", ("*","tableName"))

for line in mycursor:
  print(line)

SEE ALSO

SQL Cheat Sheet [Link]