Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
SQLALCHEMY CORE
AN INTRODUCTION
/JasonMyers @jasonamyers
Backgroundbymaul555
DIFFERENCES BETWEEN CORE AND ORM
ORM - DOMAIN MODEL
classUser(Base):
__tablename__='users'
id=Column(Integer,primary_key=True)
name=Column(String)
fullname...
CORE - SCHEMA-CENTRIC MODEL
fromsqlalchemyimportTable,Column,Integer,String,MetaData
metadata=MetaData()
users=Table('user...
STRUCTURE
Copyright©2014MochimochiLand
STRUCTURE
INSTALLING
pip installsqlalchemy
pip installflask-sqlalchemy
bin/paster create -tpyramid_alchemytutorial
INITIALIZING
importsqlalchemy
fromsqlalchemyimportcreate_engine
engine=create_engine('sqlite:///:memory:')
DEFINING A TABLE
fromsqlalchemyimportTable,Column,Integer,String,MetaData,ForeignKey
metadata=MetaData()
actors=Table('act...
CREATE THE TABLES
metadata.create_all(engine)
TABLE OBJECTS
actors.columns.items()
[
('id',Column('id',Integer(),table=actors,primary_key=True...)),
('name',Column('nam...
OPENING A CONNECTION
conn=engine.connect()
SINGLE INSERT
ins=actors.insert().values(name='Graham',fullname='GrahamChapman',body_count=3)
result=conn.execute(ins)
res...
LOOKING AT WHAT WAS EXECUTED
printstr(ins)
ins.compile().params
INSERTINTOactors(name,fullname,body_count)VALUES(:name,:fu...
MULTIPLE INSERT
results=conn.execute(roles.insert(),[
{'actor_id':1,'character_name':'KingArthur'},
{'actor_id':1,'charact...
UPDATE
stmt=actors.update().where(actors.c.name=='Graham').values(name='Gram')
result=conn.execute(stmt)
result.rowcount
1
DELETE
result=conn.execute(actors.delete().where(actors.c.name=='Terry'))
result.rowcount
1
SELECTING
s=select([actors.c.name,actors.c.fullname])
result=conn.execute(s)
forrowinresult:
printrow
(u'Graham',u'GrahamC...
ORDERING
stmt=select([actors.c.name]).order_by(actors.c.name.desc())
conn.execute(stmt).fetchall()
[(u'Terry',),(u'John',)...
LIMITING
stmt=select([actors.c.name,actors.c.fullname]).limit(1).offset(1)
conn.execute(stmt).first()
(u'John',u'JohnClees...
COUNT
fromsqlalchemy.sqlimportfunc
stmt=select([func.count(actors)])
conn.execute(stmt).scalar()
2
SUM
stmt=select([func.count(actors),func.sum(actors.c.body_count)])
conn.execute(stmt).first()
(2,5)
JOINS
s=select([actors,roles]).where(actors.c.id==roles.c.actor_id)
forrowinconn.execute(s):
printrow
(1,u'Graham',u'Graha...
GROUPING
stmt=select([actors.c.name,func.count(roles.c.id)]).
select_from(actors.join(roles)).
group_by(actors.c.name)
con...
FILTERING
fromsqlalchemy.sqlimportand_,or_,not_
stmt=select([actors.c.name,roles.c.character_name]).
where(
and_(
actors.c...
AND SO ON...
COMMON DIALECTS
Informix
MS SQL
Oracle
Postgres
SQLite
Custom
BUT WHAT IF...
classUnloadFromSelect(Executable,ClauseElement):
def__init__(self,select,bucket,access_key,secret_key):
sel...
EXAMPLE STATEMENT
unload=UnloadFromSelect(
select([fields]),
'/'.join(['s3:/',BUCKET,filename]),
ACCESS_KEY,
SECRET_KEY
)
EXAMPLE USAGE
unload(
'select*fromvenuewherevenueidin(
selectvenueidfromvenueorderbyvenueiddesclimit10)'
)
to's3://mybucke...
DYNAMIC TABLE INTROSPECTION
defbuild_table(engine,table_name):
returnTable(table_name,metadata,autoload=True,autoload_with...
CHECKING FOR NULL COLUMNS
build_table(engine,'census')
unavailable_fields=[
c.nameforcint.cifisinstance(c.type,NullType)
]
CHAINING
s=select(
[
t.c.race,
t.c.factor,
func.sum(g.t.c.value).label('summed')
],t.c.race>0
).where(
and_(
t.c.type=='PO...
CONDITIONALS
s=select(
[
table.c.discharge_year,
func.count(1).label(
'patient_discharges'),
table.c.zip_code,
],table.c.d...
s=s.group_by(table.c.zip_code)
s=s.order_by('dischargesDESC')
cases=conn.execute(s).fetchall()
QUESTIONS
THANK YOU
/JasonMyers @jasonamyers
SQLAlchemy Core: An Introduction
SQLAlchemy Core: An Introduction
SQLAlchemy Core: An Introduction
Upcoming SlideShare
Loading in …5
×

SQLAlchemy Core: An Introduction

1,185 views

Published on

My PyCon 2014 Presentation

Code, Note, and Slides at http://www.github.com/jasonamyers/pycon2014

Published in: Technology
  • Be the first to comment

SQLAlchemy Core: An Introduction

  1. 1. SQLALCHEMY CORE AN INTRODUCTION /JasonMyers @jasonamyers Backgroundbymaul555
  2. 2. DIFFERENCES BETWEEN CORE AND ORM
  3. 3. ORM - DOMAIN MODEL classUser(Base): __tablename__='users' id=Column(Integer,primary_key=True) name=Column(String) fullname=Column(String) password=Column(String)
  4. 4. CORE - SCHEMA-CENTRIC MODEL fromsqlalchemyimportTable,Column,Integer,String,MetaData metadata=MetaData() users=Table('users',metadata, Column('id',Integer,primary_key=True), Column('name',String), Column('fullname',String), )
  5. 5. STRUCTURE Copyright©2014MochimochiLand
  6. 6. STRUCTURE
  7. 7. INSTALLING pip installsqlalchemy pip installflask-sqlalchemy bin/paster create -tpyramid_alchemytutorial
  8. 8. INITIALIZING importsqlalchemy fromsqlalchemyimportcreate_engine engine=create_engine('sqlite:///:memory:')
  9. 9. DEFINING A TABLE fromsqlalchemyimportTable,Column,Integer,String,MetaData,ForeignKey metadata=MetaData() actors=Table('actors',metadata, Column('id',Integer,primary_key=True), Column('name',String), Column('fullname',String), Column('body_count',Integer) ) roles=Table('roles',metadata, Column('id',Integer,primary_key=True), Column('actor_id',None,ForeignKey('actors.id')), Column('character_name',String,nullable=False) )
  10. 10. CREATE THE TABLES metadata.create_all(engine)
  11. 11. TABLE OBJECTS actors.columns.items() [ ('id',Column('id',Integer(),table=actors,primary_key=True...)), ('name',Column('name',String(),table=actors)), ('fullname',Column('fullname',String(),table=actors)), ('body_count',Column('body_count',Integer(),table=actors)) ]
  12. 12. OPENING A CONNECTION conn=engine.connect()
  13. 13. SINGLE INSERT ins=actors.insert().values(name='Graham',fullname='GrahamChapman',body_count=3) result=conn.execute(ins) result.inserted_primary_key [1]
  14. 14. LOOKING AT WHAT WAS EXECUTED printstr(ins) ins.compile().params INSERTINTOactors(name,fullname,body_count)VALUES(:name,:fullname,:body_count) {'body_count':3,'fullname':'GrahamChapman','name':'Graham'}
  15. 15. MULTIPLE INSERT results=conn.execute(roles.insert(),[ {'actor_id':1,'character_name':'KingArthur'}, {'actor_id':1,'character_name':'VoiceofGod'}, {'actor_id':2,'character_name':'SirLancelot'}, {'actor_id':2,'character_name':'BlackKnight'}, {'actor_id':3,'character_name':'Patsy'}, {'actor_id':3,'character_name':'SirBors'}, ]) results.rowcount 6
  16. 16. UPDATE stmt=actors.update().where(actors.c.name=='Graham').values(name='Gram') result=conn.execute(stmt) result.rowcount 1
  17. 17. DELETE result=conn.execute(actors.delete().where(actors.c.name=='Terry')) result.rowcount 1
  18. 18. SELECTING s=select([actors.c.name,actors.c.fullname]) result=conn.execute(s) forrowinresult: printrow (u'Graham',u'GrahamChapman') (u'John',u'JohnCleese') (u'Terry',u'TerryGilliam')
  19. 19. ORDERING stmt=select([actors.c.name]).order_by(actors.c.name.desc()) conn.execute(stmt).fetchall() [(u'Terry',),(u'John',),(u'Graham',)]
  20. 20. LIMITING stmt=select([actors.c.name,actors.c.fullname]).limit(1).offset(1) conn.execute(stmt).first() (u'John',u'JohnCleese')
  21. 21. COUNT fromsqlalchemy.sqlimportfunc stmt=select([func.count(actors)]) conn.execute(stmt).scalar() 2
  22. 22. SUM stmt=select([func.count(actors),func.sum(actors.c.body_count)]) conn.execute(stmt).first() (2,5)
  23. 23. JOINS s=select([actors,roles]).where(actors.c.id==roles.c.actor_id) forrowinconn.execute(s): printrow (1,u'Graham',u'GrahamChapman',1,1,u'KingArthur') (1,u'Graham',u'GrahamChapman',2,1,u'VoiceofGod') (2,u'John',u'JohnCleese',3,2,u'SirLancelot') (2,u'John',u'JohnCleese',4,2,u'BlackKnight') (3,u'Terry',u'TerryGilliam',5,3,u'Patsy') (3,u'Terry',u'TerryGilliam',6,3,u'SirBors')
  24. 24. GROUPING stmt=select([actors.c.name,func.count(roles.c.id)]). select_from(actors.join(roles)). group_by(actors.c.name) conn.execute(stmt).fetchall() [(u'Graham',2),(u'John',2),(u'Terry',2)]
  25. 25. FILTERING fromsqlalchemy.sqlimportand_,or_,not_ stmt=select([actors.c.name,roles.c.character_name]). where( and_( actors.c.name.like('Gra%'), roles.c.character_name.like('Vo%'), actors.c.id==roles.c.actor_id ) ) conn.execute(stmt).fetchall() [(u'Graham',u'VoiceofGod')]
  26. 26. AND SO ON...
  27. 27. COMMON DIALECTS Informix MS SQL Oracle Postgres SQLite Custom
  28. 28. BUT WHAT IF... classUnloadFromSelect(Executable,ClauseElement): def__init__(self,select,bucket,access_key,secret_key): self.select=select self.bucket=bucket self.access_key=access_key self.secret_key=secret_key @compiles(UnloadFromSelect) defvisit_unload_from_select(element,compiler,**kw): return"unload('%(query)s')to'%(bucket)s' credentials'aws_access_key_id=%(access_key)s; aws_secret_access_key=%(secret_key)s'delimiter',' addquotesallowoverwrite"%{ 'query':compiler.process(element.select, unload_select=True,literal_binds=True), 'bucket':element.bucket, 'access_key':element.access_key, 'secret_key':element.secret_key, }
  29. 29. EXAMPLE STATEMENT unload=UnloadFromSelect( select([fields]), '/'.join(['s3:/',BUCKET,filename]), ACCESS_KEY, SECRET_KEY )
  30. 30. EXAMPLE USAGE unload( 'select*fromvenuewherevenueidin( selectvenueidfromvenueorderbyvenueiddesclimit10)' ) to's3://mybucket/venue_pipe_' credentials'aws_access_key_id=ACCESS_KEY; aws_secret_access_key=SECRET_KEY';
  31. 31. DYNAMIC TABLE INTROSPECTION defbuild_table(engine,table_name): returnTable(table_name,metadata,autoload=True,autoload_with=engine)
  32. 32. CHECKING FOR NULL COLUMNS build_table(engine,'census') unavailable_fields=[ c.nameforcint.cifisinstance(c.type,NullType) ]
  33. 33. CHAINING s=select( [ t.c.race, t.c.factor, func.sum(g.t.c.value).label('summed') ],t.c.race>0 ).where( and_( t.c.type=='POVERTY', t.c.value!=0 ) ).group_by( t.c.race, t.c.factor ).order_by( t.c.race, t.c.factor)
  34. 34. CONDITIONALS s=select( [ table.c.discharge_year, func.count(1).label( 'patient_discharges'), table.c.zip_code, ],table.c.discharge_year.in_(years) ).group_by(table.c.discharge_year) s=s.where(table.c.hospital_name==provider) if'total_charges'notinunavailable_fields: s=s.column( func.sum(table.c.total_charges ).label('patient_charges') )
  35. 35. s=s.group_by(table.c.zip_code) s=s.order_by('dischargesDESC') cases=conn.execute(s).fetchall()
  36. 36. QUESTIONS THANK YOU /JasonMyers @jasonamyers

×