from sqlalchemy import create_engine
from sqlalchemy.engine.url import URL
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql.expression import text as Text
def db_connect(db):
return create_engine(URL(**db)) # db should be a dict with connection params
db = {'drivername': 'postgresql', 'host': 'localhost', 'port': '5432',
'username': 'username', 'password': 'pw', 'database': 'db_name'}
session = sessionmaker(bind=db_connect(db))()
query = Text('SQL QUERY')
for row in session.execute(query).fetchall():
# do stuff with the data where type(row) == sqlalchemy.engine.result.RowProxy
session.close()