Skip to content

Instantly share code, notes, and snippets.

@iolloyd
Last active September 18, 2015 10:16
Show Gist options
  • Save iolloyd/f33984f31df4375c9300 to your computer and use it in GitHub Desktop.
Save iolloyd/f33984f31df4375c9300 to your computer and use it in GitHub Desktop.
optionally get column names with result set
from sqlalchemy import create_engine
def db_results(conn, sql, columns=True):
x = conn.execute(sql)
out = x.fetchall()
if columns:
out = [dict(zip(x.keys(), a)) for a in out]
return out
engine = create_engine('mssql+pymssql://user:[email protected]:1433/my_db')
connection = engine.connect()
sql = 'select things from some_table'
result = db_results(connection, sql)
print result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment