Python + MySQL is the bread-and-butter combination of data science: the database stores the data, Python analyses it. The connection takes five lines of code.
Key Points
- Install the driver: pip install mysql-connector-python
- Connect: conn = mysql.connector.connect(host, user, password, database)
- Run queries via cursor: cur.execute('SELECT …'); rows = cur.fetchall()
- Always parameterise: cur.execute('SELECT … WHERE id=%s', (uid,)) — never f-strings (SQL injection)
- Commit writes with conn.commit(); close cursors and connections
- Alternatives: PyMySQL (pure Python) and SQLAlchemy (ORM + engine layer)
.png)