This document discusses SQLAlchemy, a Python library for accessing relational databases. It provides an object-relational mapper (ORM) that allows Python objects to be mapped to database tables. Key features discussed include SQL expression, which allows SQL queries to be constructed programmatically in Python, and the unit of work pattern, where the ORM session tracks object states and generates SQL statements. The document also covers getting started with SQLAlchemy, including installing it, defining models, connecting to a database, querying models, and using sessions and transactions.
4. Why I use SQLAlchemy
I Don’t Know Other OR Mapper with Python.
I Love SQL And Python!
SQLAlchemy has SQL Expression That makes
SQL Python Object.
SQLAlchemy doesn’t keep me away from SQL.
10. OR Mapper
mapper(User, users)
user = Session.query(User).filter(
User.first_name == u’Atsushi’).first()
user.first_name = u’aodag’
Session.flush()
11. Unit Of Work
There are no save method.
Session object manages Object states.
That calls DML depending on the object state.
12. Unit Of Work
user = User(u’Atsushi’, u’Odagiri’)
Session.add(user)
user.company = Company(u’BeProud’)
Session.flush()
user.first_name = u’aodag’
Session.flush()