SQLAlchemy is a Python SQL toolkit and object relational mapper that provides a full-featured SQL expression language while also allowing direct SQL execution. It features a core component that handles engine, connection, dialect, metadata, table, and column objects to work with databases, along with an ORM that builds on the core to allow mapping classes to tables with relationships and querying. SQLAlchemy supports all major databases including PostgreSQL, MySQL, MSSQL, SQLite, Sybase, Drizzle, Firebird, and Oracle.
SQLAlchemy
Core
Dialect
Defines the behaviorof a specific
database and DB-API combination.
Query generation, execution, result
handling, anything that differs from
other dbs is handled in Dialect.
SQLAlchemy
Core
More on Columns
Columnshave some important
parameters.
index=bool, nullable=bool,
unique=bool, primary_key=bool,
default=callable/scalar,
onupdate=callable/scalar,
autoincrement=bool
SQLAlchemy
Core
Select A LittleBit Fancy
select([func.count(ct.c.id).label('co
unt'),
ct.c.region]).group_by(ct.c.region).o
rder_by('count DESC')
SELECT count(countries.id) AS count,
countries.region
FROM countries GROUP BY
countries.region ORDER BY count DESC
SQLAlchemy
Core
Cooler Update
case_list =[(pt.c.id == photo_id,
index+1) for index, photo_id in
enumerate(order_list)]
pt.update().values(photo_order=case(case_l
ist))
UPDATE photos SET photo_order=CASE WHEN
(photos.id = :id_1) THEN :param_1 WHEN
(photos.id = :id_2) THEN :param_2 END