You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use select function which returns an instance of sqlalchemy.sql.selectable.Select which can be passed into conn.execute
This returns a ResultProxy which can be iterated through like so:
In [37]: for r in result: print r
(1, u'bill', u'jacky hacky')
(2, u'john', u'john p')
(3, u'lex', u'Hello')
(4, u'john', u'john magoo')
(5, u'bob', u'bobo')
Can access the values using array-indexes, dict keys or attributes, as follows:
In [41]: result = conn.execute(s).fetchone()
In [42]: result['name']
Out[42]: u'bill'
In [43]: result.name
Out[43]: u'bill'
In [44]: result[1]
Out[44]: u'bill'
Call result.close() to return connection to connection pool.
Use .where() called against the Select object to add filter expressions
Operators
Using Python operators again Column objects, will generator SQL equivalent:
In [45]: print users.c.id == addresses.c.id
users.id = addresses.id
In [47]: print users.c.id != None
users.id IS NOT NULL
In [48]: print users.c.id == None
users.id IS NULL
Can generator operators using .op('OPERATOR')(value) method
Conjunctions
Import from sqlalchemy.sql to use
In [59]: from sqlalchemy.sql import and_, or_, not_
In [60]: print or_(users.c.id < 100, users.c.id > 2)
users.id < :id_1 OR users.id > :id_2
In [61]: print and_(users.c.id < 100, users.c.id.like('%l'))
users.id < :id_1 AND users.id LIKE :id_2
The where method can be chained
Using Joins
Aside from manually performing joins, they can also be performed with the join method.