Explore the seamless integration of Python and MySQL databases through SQLAlchemy ORM. Learn how to leverage Python's powerful tools and best practices for efficient data management, combining the simplicity of MySQL with the flexibility of SQLAlchemy for enhanced database operations.

• Python 3
• Python 3.12

Equipment list

Here you can find the list of equipment used to create this tutorial.

This link will also show the software list used to create this tutorial.

Related tutorial - Python

On this page, we offer quick access to a list of tutorials related to Python.

What is ORM?

ORM stands for Object-Relational Mapping. It is a programming technique that enables you to convert data between incompatible systems in object-oriented programming languages. In the context of databases, ORM is used to map objects to tables in a relational database management system.

Why use ORM in Python?

ORM in Python provides a convenient way to interact with databases by abstracting the underlying SQL statements and allowing developers to work with objects directly in their code. This simplifies database operations, enhances code portability, improves security by preventing SQL injection attacks, facilitates code maintenance by providing a clear and concise syntax for database queries, and enables easier unit testing by allowing objects to be manipulated in memory without the need for separate database setups.

What is SQLAlchemy?

SQLAlchemy is an open-source SQL toolkit and Object-Relational Mapping (ORM) library for the Python programming language. It provides a comprehensive set of tools for working with relational databases, allowing developers to interact with databases using Python objects. SQLAlchemy's core provides a SQL expression language that allows the creation and execution of SQL statements, while its ORM facilitates the mapping of Python classes to database tables, enabling developers to work with database rows as objects.

Tutorial Python - MySQL database using SQLAlchemy ORM

Install the MySQL database.

Copy to Clipboard

In our example, we installed it on a computer running Linux.

Access the MySQL service as the ROOT user.

Copy to Clipboard

Create a database.

Copy to Clipboard

Create a user account.

Copy to Clipboard

Grant privileges over the database created to this user account.

Copy to Clipboard

The MY_PYTHON_USER account will be allowed to access the DRAGONBALLZ database.

Test the MySQL connection to the database using the new account.

Copy to Clipboard

On your desktop, install SQLAlchemy using PIP.

Copy to Clipboard

Install the required Python package to connect to the MySQL server.

Copy to Clipboard

Create a Python file.

Copy to Clipboard

Import modules and features from SQLAlchemy.

Copy to Clipboard

Establish a connection to the database.

Copy to Clipboard

The SQLAlchemy engine is used to establish a connection to the database.

The ECHO argument is optional and is used to display on the screen all SQL operations performed.

Create a base class to declare models.

Copy to Clipboard

Define a model for the table.

Copy to Clipboard

Create the tables in the database.

Copy to Clipboard

This creates a table in the connected MySQL database. It defines the table's structure with five columns: ID as the primary key set to auto-increment, NAME, SPECIAL_MOVE, SS_LEVEL, and EYE_COLOR.

Before any interaction with the database, a session must be created.

Copy to Clipboard

Insert data in the table.

Copy to Clipboard

Save changes to the table.

Copy to Clipboard

Query all information stored in the table.

Copy to Clipboard

Display the information retrieved.

Copy to Clipboard

Here is the command output.

Copy to Clipboard

Display the information retrieved as a formatted string.

Copy to Clipboard

Here is the command output.

Copy to Clipboard

Optionally, query only for specific fields.

Copy to Clipboard

Here is the command output.

Copy to Clipboard

Close the connection to the database.

Copy to Clipboard

Here is the complete Python script.

Copy to Clipboard

Conclusion

Incorporating SQLAlchemy ORM simplifies MySQL database management in Python, enhancing data manipulation efficiency and offering a seamless object-oriented experience. Embrace robust data operations with ease.