Introduction to Databases in Python
Jason Myers
Co-Author of Essential SQLAlchemy and Software Engineer
SELECT column_name FROM table_name
SELECT pop2008 FROM PeopleSELECT * FROM Peoplefrom sqlalchemy import create_engineengine = create_engine('sqlite:///census_nyc.sqlite')connection = engine.connect()stmt = 'SELECT * FROM people'result_proxy = connection.execute(stmt)results = result_proxy.fetchall()
result_proxy = connection.execute(stmt)
results = result_proxy.fetchall()
result_proxy is a ResultProxyresults is a ResultSetfirst_row = results[0]
print(first_row)
('Illinois', 'M', 0, 89600, 95012)
print(first_row.keys())
['state', 'sex', 'age', 'pop2000', 'pop2008']
print(first_row.state)
'Illinois'
from sqlalchemy import Table, MetaData metadata = MetaData()census = Table('census', metadata, autoload=True, autoload_with=engine)stmt = select([census])results = connection.execute(stmt).fetchall()
stmt = select([census])print(stmt)
'SELECT * from CENSUS'
Introduction to Databases in Python