This is amazingly well-timed. Just yesterday I was complaining that the Python SQL libraries are annoying and verbose, and that I wished there was a library as good as requests is for HTTP.
def query(connection, q):
cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
cursor.execute(q)
return iter(cursor.fetchone, None)
# db = records.Database('postgres://...')
db = psycopg2.connect('postgres://...')
# rows = db.query('select * from active_users')
rows = query(db, 'select * from active_users')
# rows.next()
next(rows)
# for row in rows:
# spam_user(name=row['name'], email=row['user_email'])
for row in rows:
spam_user(name=row['name'], email=row['user_email'])
# rows.all()
list(rows)