Last active
September 18, 2015 10:16
-
-
Save iolloyd/f33984f31df4375c9300 to your computer and use it in GitHub Desktop.
optionally get column names with result set
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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