Psycopg

from contextlib import contextmanager
import psycopg2

@contextmanager
def dbcur():
    con = psycopg2.connect("postgresql:///foo")
    cur = con.cursor()
    try:
        yield cur
    finally:
        con.commit()
        cur.close()
        con.close()


with dbcur() as cur:
    cur.execute("SELECT * FROM users WHERE id = %s", [1])
    print(cur.fetchone())