Para usar MySQL em Python é preciso instalar o pacote MySQL pip install mysql-connector-python
.
Faz-se a ligação à base de dados e depois executa-se as queries.
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database="mydatabase"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT MAX(id) FROM fiscal_years")
fiscal_year_id = mycursor.fetchone()[0]
mycursor.execute("SELECT CAST(start_period AS DATE) FROM fiscal_years WHERE id = %s", (fiscal_year_id,))
elite_since = mycursor.fetchone()[0]
mycursor.execute("SELECT MAX(fiscal_year) FROM fiscal_years")
fiscal_year = mycursor.fetchone()[0]
print(fiscal_year_id)
print(elite_since)
print(fiscal_year)