Skip to content

Instantly share code, notes, and snippets.

@sunpig
Created February 4, 2014 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunpig/8803576 to your computer and use it in GitHub Desktop.
Save sunpig/8803576 to your computer and use it in GitHub Desktop.
from sqlalchemy import MetaData, create_engine, orm
from sqlalchemy.sql import select
engine = create_engine('postgresql+psycopg2://username:password@localhost:5432/dbname')
Session = orm.sessionmaker(bind=engine)
session = Session()
metadata = MetaData(bind=engine)
metadata.reflect()
table_1 = metadata.tables['table_1']
table_1_column_1 = table_1.columns['column_1']
labelled_column = table_1_column_1.label('my test column')
query = select([labelled_column]).select_from(table_1).order_by(labelled_column)
query = session.query(table_1).select(table_1_column_1)
str(query) # shows the SQL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment