3 ms·
In SQLAlchemy, you can choose to use only the query builder without using the ORM part at all. The query builder in SQLAlchemy is very well documented[1] and it
by sirn 6y ago
In SQLAlchemy, you can choose to use only the query builder without using the ORM part at all. The query builder in SQLAlchemy is very well documented[1] and its use without ORM has the same level of support as its ORM counterpart. SQLAlchemy call their query builder a SQL Expression Language[2], e.g.
# Boilerplate
from sqlalchemy import create_engine, MetaData, Table
engine = create_engine('sqlite:///:memory:', echo=True)
metadata = MetaData()
customers = Table('customers', metadata, autoload_with=engine)
query = customers.select([customers.c.id, customers.c.fname, customers.c.lname, customers.c.phone])
# => SELECT customers.id, customers.fname, customers.lname, customers.phone FROM customers
conn = engine.connect()
conn.execute(query)
# => [(1, "Foo", "Bar", "12345678"), ...]
ActiveRecord also has Arel which can be use as a standalone. Documentation is a bit more sparse compared to SQLAlchemy Core or its ActiveRecord ORM counterpart, though.
[1]: https://docs.sqlalchemy.org/en/13/index.html https://docs.sqlalchemy.org/en/13/index.html
[1]: https://docs.sqlalchemy.org/en/13/core/tutorial.html https://docs.sqlalchemy.org/en/13/core/tutorial.html