Created
May 11, 2017 10:10
-
-
Save lokori/d9126f4288fc09209611aa8e6dcd5cd7 to your computer and use it in GitHub Desktop.
run to the db hills
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
| # Run something in a db transaction, handle commit etc. | |
| # f is a function which takes db cursor as a parameter for callback | |
| def run_with_db(f): | |
| connection = None | |
| try: | |
| connection = psycopg2.connect(__get_connection_string(config)) | |
| cursor = connection.cursor() | |
| rv = f(cursor) # run something in db transaction | |
| cursor.close() | |
| return rv | |
| except psycopg2.DatabaseError, exception: | |
| print exception | |
| sys.exit(1) | |
| finally: | |
| if connection: | |
| connection.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment