Databases & Storage

SQLAlchemy

Lets Python code talk to almost any database without learning that database's own dialect.

Install it: pip install sqlalchemy

What does it do?

SQLAlchemy lets a Python program work with a database — storing, retrieving, and updating information — without necessarily writing raw SQL, the specialized language databases normally speak. Instead, it lets you represent database tables as ordinary Python objects and their relationships as ordinary Python code, then translates your instructions into whichever specific dialect of SQL the actual database underneath understands. That means the same Python code can often work against PostgreSQL, MySQL, or SQLite with only minor changes, since SQLAlchemy handles the differences between them. It also offers a lower-level mode for developers who want to write more direct SQL while still getting its connection-management and safety features.

See it in action

This code connects to a database file, creates a simple table, adds one row of data, and then prints everything stored in that table.

from sqlalchemy import create_engine, text

engine = create_engine("sqlite:///example.db")

with engine.connect() as conn:
    conn.execute(text("CREATE TABLE IF NOT EXISTS users (id INTEGER, name TEXT)"))
    conn.execute(text("INSERT INTO users VALUES (1, 'Alice')"))
    conn.commit()
    result = conn.execute(text("SELECT * FROM users"))
    print(result.fetchall())

Why would a non-developer care?

Nearly every piece of software that remembers anything between visits — your saved settings, an online store’s inventory, a social app’s list of your friends — is backed by a database, and SQLAlchemy is one of the most common ways Python software talks to one. It matters because it removes a huge category of tedious, error-prone code and reduces the risk of security bugs like SQL injection that come from carelessly hand-building database queries. Switching database technology later, a common and painful problem, becomes far less painful when SQLAlchemy sits in between.

Real-world examples

SQLAlchemy is one of the most widely used database libraries in the Python world and sits underneath countless companies’ backend systems, from startups to large enterprises, often paired with web frameworks like Flask. It was created by Mike Bayer in 2005 and has been actively developed for two decades, an unusually long lifespan for infrastructure software. Without an abstraction layer like it, switching a company’s application from one database system to another would typically mean rewriting large chunks of database code by hand.

Who uses it

Backend developers building any application that needs to reliably store and query data in a real database, from small startups to large enterprises.

How it compares to alternatives

Django, the popular web framework, ships with its own separate built-in ORM instead of SQLAlchemy, reflecting a long-standing philosophical split in the Python community. Peewee offers a much lighter, simpler alternative for smaller projects that don’t need SQLAlchemy’s full power. Tortoise ORM offers similar ideas but built natively for async code from the ground up.

New to Python and want to actually try libraries like this yourself?

Find a beginner-friendly course

Related libraries