Introduction to Databases in Python
Jason Myers
Co-Author of Essential SQLAlchemy and Software Engineer
stmt = select([census.columns.pop2008, state_fact.columns.abbreviation])
results = connection.execute(stmt).fetchall() print(results)
[(95012, u'IL'),
(95012, u'NJ'),
(95012, u'ND'),
(95012, u'OR'),
(95012, u'DC'),
(95012, u'WI'),
...
select()
clause and prior to any where()
, order_by()
or group_by()
clausesjoin()
clausestmt = select([func.sum(census.columns.pop2000)])
stmt = stmt.select_from(census.join(state_fact))
stmt = stmt.where(state_fact.columns.circuit_court == '10')
result = connection.execute(stmt).scalar() print(result)
14945252
stmt = select([func.sum(census.columns.pop2000)])
stmt = stmt.select_from( census.join(state_fact, census.columns.state == state_fact.columns.name))
stmt = stmt.where( state_fact.columns.census_division_name == 'East South Central')
result = connection.execute(stmt).scalar() print(result)
16982311
Introduction to Databases in Python