Master MySQL with Python: Simple Example Tutorial for Beginners

Logan Jul 13, 2026

Python and MySQL are powerful tools when used together. Python, a versatile programming language, can interface with MySQL, a popular open-source relational database management system, to fetch, manipulate, and store data. Let's explore an example of using Python with MySQL to connect to a database and fetch data.

Python MySQL WHERE Clause Examples
Python MySQL WHERE Clause Examples

Before we dive in, ensure you have the mysql-connector-python package installed. You can install it using pip: pip install mysql-connector-python.

Ultimate Python Cheat Sheet for Beginner
Ultimate Python Cheat Sheet for Beginner

Establishing a Connection

The first step is to establish a connection with your MySQL server. Here's a simple connection setup:

Python Cheatsheet
Python Cheatsheet

```python import mysql.connector cnx = mysql.connector.connect( user='username', password='password', host='localhost', database='database' ) ```

Using cursor to interact with the database

an image of a black background with the words credit card variation in python
an image of a black background with the words credit card variation in python

A cursor object is used to execute SQL commands and fetch results. Here's how to use it:

```python cursor = cnx.cursor() cursor.execute("SHOW DATABASES") for db in cursor: print(db) ```

Fetching data specifically

All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)

You can fetch data from a specific table with a SELECT statement. Let's fetch some data from a 'users' table:

```python cursor.execute("SELECT * FROM users") for user in cursor: print(user) ```

Manipulating Data

Python List Examples
Python List Examples

Python can also be used to insert, update, and delete data in your MySQL database.

Inserting data

Python Tuples
Python Tuples
Python Cheat Sheet for Beginners
Python Cheat Sheet for Beginners
an image of a computer screen with the text'generation ascii art in python '
an image of a computer screen with the text'generation ascii art in python '
the calculator factor in python is displayed on a blackboard with numbers and letters
the calculator factor in python is displayed on a blackboard with numbers and letters
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Python Cheat Sheet for Beginners 2026 | Python Basics, Syntax, Loops, Functions & Variables
Export Database To CSV File In Python
Export Database To CSV File In Python
Python If Else Examples
Python If Else Examples
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
Python Data Structures Cheat Sheet for Beginners (Lists, Tuples, Sets & Dictionaries)
a computer screen with the words python string method
a computer screen with the words python string method
three different types of python programming
three different types of python programming
a screenshot of a computer screen with the text looping through dictionaries in python
a screenshot of a computer screen with the text looping through dictionaries in python
"6 Python List Methods Every Beginner Should Know"
"6 Python List Methods Every Beginner Should Know"
Python & SQL ✅
Python & SQL ✅
an important function in python is to display text or numbers and symbols on the table
an important function in python is to display text or numbers and symbols on the table
Day 27: NumPy in Python for Beginners 🧮
Day 27: NumPy in Python for Beginners 🧮
Python List Methods
Python List Methods
required functions
required functions
a black background with an image of a rabbit on it's face and the words python
a black background with an image of a rabbit on it's face and the words python
Python array sorting guide #python #programming #tutorial
Python array sorting guide #python #programming #tutorial
Python For Everything – Top Libraries & What They’re Used For
Python For Everything – Top Libraries & What They’re Used For

Let's insert a new user into the 'users' table:

```python cursor.execute("INSERT INTO users (name, email) VALUES (%s, %s)", ("John Doe", "john@example.com")) cnx.commit() ```

Updating data

Now let's update this user's email:

```python cursor.execute("UPDATE users SET email=%s WHERE name=%s", ("john.doe@example.com", "John Doe")) cnx.commit() ```

Incorporating Python and MySQL allows for efficient data management, easy data extraction, and automated data processing. This opens up numerous possibilities for data-driven applications. Now that you have a basic understanding, explore further to see what you can create with these powerful tools!