Your SlideShare is downloading. ×
SQLAlchemy Core: An Introduction
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

SQLAlchemy Core: An Introduction

400
views

Published on

My PyCon 2014 Presentation …

My PyCon 2014 Presentation

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

Published in: Technology

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
400
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
13
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. SQLALCHEMY CORE AN INTRODUCTION /JasonMyers @jasonamyers Backgroundbymaul555
  • 2. DIFFERENCES BETWEEN CORE AND ORM
  • 3. ORM - DOMAIN MODEL classUser(Base): __tablename__='users' id=Column(Integer,primary_key=True) name=Column(String) fullname=Column(String) password=Column(String)
  • 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. STRUCTURE Copyright©2014MochimochiLand
  • 6. STRUCTURE
  • 7. INSTALLING pip installsqlalchemy pip installflask-sqlalchemy bin/paster create -tpyramid_alchemytutorial
  • 8. INITIALIZING importsqlalchemy fromsqlalchemyimportcreate_engine engine=create_engine('sqlite:///:memory:')
  • 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. CREATE THE TABLES metadata.create_all(engine)
  • 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. OPENING A CONNECTION conn=engine.connect()
  • 13. SINGLE INSERT ins=actors.insert().values(name='Graham',fullname='GrahamChapman',body_count=3) result=conn.execute(ins) result.inserted_primary_key [1]
  • 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. 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. UPDATE stmt=actors.update().where(actors.c.name=='Graham').values(name='Gram') result=conn.execute(stmt) result.rowcount 1
  • 17. DELETE result=conn.execute(actors.delete().where(actors.c.name=='Terry')) result.rowcount 1
  • 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. ORDERING stmt=select([actors.c.name]).order_by(actors.c.name.desc()) conn.execute(stmt).fetchall() [(u'Terry',),(u'John',),(u'Graham',)]
  • 20. LIMITING stmt=select([actors.c.name,actors.c.fullname]).limit(1).offset(1) conn.execute(stmt).first() (u'John',u'JohnCleese')
  • 21. COUNT fromsqlalchemy.sqlimportfunc stmt=select([func.count(actors)]) conn.execute(stmt).scalar() 2
  • 22. SUM stmt=select([func.count(actors),func.sum(actors.c.body_count)]) conn.execute(stmt).first() (2,5)
  • 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. 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. 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. AND SO ON...
  • 27. COMMON DIALECTS Informix MS SQL Oracle Postgres SQLite Custom
  • 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. EXAMPLE STATEMENT unload=UnloadFromSelect( select([fields]), '/'.join(['s3:/',BUCKET,filename]), ACCESS_KEY, SECRET_KEY )
  • 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. DYNAMIC TABLE INTROSPECTION defbuild_table(engine,table_name): returnTable(table_name,metadata,autoload=True,autoload_with=engine)
  • 32. CHECKING FOR NULL COLUMNS build_table(engine,'census') unavailable_fields=[ c.nameforcint.cifisinstance(c.type,NullType) ]
  • 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. 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. s=s.group_by(table.c.zip_code) s=s.order_by('dischargesDESC') cases=conn.execute(s).fetchall()
  • 36. QUESTIONS THANK YOU /JasonMyers @jasonamyers