Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Getting Started With SQLAlchemy

00:00 In this lesson, you’ll shift your focus from raw SQL to SQLAlchemy, which gives you a cleaner and more friendly way to work with databases. You’re going to start off by creating a new virtual environment and installing SQLAlchemy.

00:15 Next, you’ll learn how an Object-Relational Mapper, or ORM, is used to define the base class and model. From there, you’ll connect to the birds.db database and automatically generate your tables.

00:27 Everything we build here will support the CRUD operations you’ll perform in the next lesson. Let’s take a moment to understand what an Object-Relational Mapper actually does. In the previous lessons, you’ve used raw SQL to write the queries yourself.

00:43 But with an ORM, those details are handled automatically, and Python writes the queries for you. ORMs allow you to work with classes and objects instead of SQL commands, which makes your code cleaner, safer, and much easier to maintain.

01:00 SQLAlchemy is the most popular and widely used ORM. It gives you a powerful and flexible way to work with databases using Python-native concepts. One of the biggest benefits of working with ORMs is readability. For example, instead of writing a full SQL UPDATE statement, such as UPDATE bird SET name equal to 'Sparrow', where id is equal to 1, you’ll simply update an attribute by setting bird.name to "Sparrow".

01:31 Before working with SQLAlchemy, you need to create a new virtual environment using the venv built-in module. To do this, you’re going to open up your command prompt, navigate to your project directory, and type python -m venv, and if you want you can give it a custom name, but I’ll just go with venv.

01:53 Next, you’ll type out the second line .\ venv\Scripts\ activate. This creates and activates a new virtual environment in your designated project folder, which ensures that all of your dependencies stay isolated to this project. In this way, SQLAlchemy and any future libraries won’t interfere with other Python work on your system. Next, you’ll install SQLAlchemy inside your virtual environment using pip by typing python -m pip install SQLAlchemy.

02:32 Once the installation is complete, you’ll have access to SQLAlchemy’s ORM tools.

02:39 Now you’re ready for the main setup file. The first thing you need to do is create a new Python file called crud_sql_alchemy.py.

02:49 Next, you import the pieces of SQLAlchemy that you’ll need.

02:54 create_engine() will connect SQLAlchemy to a database. Column, Integer, and String will be used to define table columns.

03:05 DeclarativeBase is used to create the base class for all ORM models. And sessionmaker() creates session objects, which handle read and write operations.

03:16 From here, you’ll define the base class. Every ORM model you create going forward will inherit from this base class, and SQLAlchemy will use it to track model metadata and generate tables automatically.

03:31 Now you define the Bird model.

03:35 This class represents a table named bird in the database. And each attribute that you list here will become a table column.

03:50 Next, you’ll define a .__repr__() method to get readable output when printing model instances.

04:01 You’ll create an engine to connect to your SQLite database. And define the session, which will allow you to interact with the database.

04:16 Finally, you’ll define the init_db() function to initialize the database. This function looks at all ORM models that inherit from the base class, such as your Bird model, and automatically generates their tables inside the database if they don’t yet exist, which means you no longer have to write raw SQL to create your tables.

04:38 As the last step, be sure to save this file in your project folder, then run the script from your terminal.

04:47 Well done! Your SQLAlchemy environment is now fully set up.

Become a Member to join the conversation.