Write Python Instead of SQL!: An Introduction To Sqlalchemy
Write Python Instead of SQL!: An Introduction To Sqlalchemy
An introduction to
SQLAlchemy.
Plain DBAPI+SQL
>>> import psycopg2
>>> conn = psycopg2.connect('dbname=test')
>>> cur = conn.cursor()
>>> cur.execute("CREATE TABLE test (id serial PRIMARY KEY, data varchar)")
>>> cur.execute("INSERT INTO test VALUES (%s)", ("Little Bobby 'tables'",))
>>> cur.execute("INSERT INTO test VALUES ('%s')" % ("Little Bobby 'tables'",))
ProgrammingError: syntax error at or near "tables"
LINE 1: INSERT INTO test (data) VALUES ('Little Bobby 'tables'')
SQLAlchemy (low-level)
>>> from sqlalchemy import *
>>> metadata = MetaData()
>>> table = Table('test', metadata,
Column('id', Integer, primary_key=True),
Column('name', String))
>>> engine = create_engine('postgresql:///test')
>>> metadata.create_all(engine)
>>> conn = engine.connect()
>>> table.insert(bind=engine).values(name="Little Bobby 'tables'").execute()
>>> conn.execute(select([table])).fetchone()
(1, u"Little Bobby 'tables'")
SQLAlchemy (ORM)
>>> from sqlalchemy import *
>>> from sqlalchemy.ext.declarative import declarative_base
>>> Base = declarative_base()
>>> class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String)
def __repr__(self):
return '<User({}, {})>'.format(self.id, self.name)
>>> engine = create_engine('postgresql:///test')
>>> Base.metadata.create_all(engine)
SQLAlchemy (ORM)
>>> from sqlalchemy.orm import sessionmaker
>>> session = sessionmaker(bind=engine)()
>>> session.query(User).one()
<User(1, Bobby Tables)>
>>> u
<User(1, None, Bobby Tables)>
>>> u2
<User(2, None, Vimmy McVimface)>
>>> g
<Group(1, Scriptkiddies)>
Let’s have a relationship
>>> u.group = g
>>> g.users
[<User(1, <Group(1, Scriptkiddies)>, Bobby Tables)>]
>>> g.users.append(u2)
>>> u2
<User(2, <Group(1, Scriptkiddies)>, Vimmy McVimface)>
>>> g.users
[<User(1, <Group(1, Scriptkiddies)>, Bobby Tables)>,
<User(2, <Group(1, Scriptkiddies)>, Vimmy McVimface)>]
What else is there
Query options to control column/relationship loading
Cascading along relationships
Custom types
Signals
Why use SQLAlchemy again?
Sacrifice a little bit of performance / SQL purity for
much faster development
Cleaner / shorter code
Basically no SQL injection risks
Excellent documentation http://docs.sqlalchemy.org/en/rel_1_0/
Very helpful community (#sqlalchemy on freenode)