Create API in Python MySQL

Python and MySQL? The world of relational databases is present in the vast majority of companies.

After all, there are several advantages linked to the operation of transactional systems commonly associated with SQL Database. That’s why this project unites two widely used worlds: Python and MySQL!

If you need to better understand what the relational model is about, I strongly recommend reading articles related to the topic , here, on our website.

We are always adding new content on the subject!

Database Posts:

In addition, Python is one of the main general purpose languages most used by the market, as well as by developers.

So, let’s go with these two worlds together? Let’s create our Python API with MySQL

Getting to know the Project

Very well, what will we create with python and mysql? There are several possibilities. However, our focus is on accessing data persisted in MySQL.

So, let’s build an API using fastapi to access and manipulate the data stored in MySQL!

This project from playlist on Youtube, from the channel SR from Python with MySQL.

The objective of this project is to create a connection class and CRUD methods for retrieving, manipulating and removing data from the MySQL database in a local instance.

You will notice that some methods were created for teaching purposes. For these, as well as others, you can make your own modifications.

Libraries used in the project

  • python-dotevn
  • os
  • python-mysql-connector
  • Fastapi

First Step – Create MySQL connection class with Python

In this first video of the database project with python we will create the MySQL connection class using the mysql-connector-python connector.

This way we will be able to retrieve data from the MySQL database with Python.

After the connection is made, we need to create the data manipulation methods.

Second Step – Coding the Application’s CRUD operations in Python

How can we retrieve data from the DBMS using Python? In this part 1 of CRUD we will code the first MySQL data recovery methods.

Information such as available tables and database maintenance methods.

In part 2, we will code the rest of CRUD. Implement other READ methods, in addition to creating CREATE, UPDATE and DELETE data within a table in MySQL.

Remember that the objective of the mini project is to create an API for consuming data via URL with FASTapi. Python and MySQL! So, let’s go!

In the next video we will continue to retrieve data from mysql using Python.

In this part 2 of CRUD we will check some ways to create queries for any table or attribute in the database.

As I suggested in the video, tell me in the comments how you changed the data update method. Did it make an error? Tell me. Did everything run smoothly? Wonderful.

Tell me if this type of content helps you, so we can produce more similar content.

Final – Building the API with FastAPI to access MySQL

In this class we will modify the previous code to create an API using Fastapi with the application’s main CRUD methods.

At the end, we will test to check if everything is in compliance!

For testing we will use the browser itself in conjunction with the postman program to test the HTTP methods.

Finally, we did it! We created our API with Python and MySQL!

Feel free to make your own changes to the code!

To access all classes, part of a project for a Python portfolio with MySQL, access the complete project, access playlist.

Juliana Mascarenhas

Data Scientist and Master in Computer Modeling by LNCC.
Computer Engineer