Created
November 12, 2018 02:27
-
-
Save gsweene2/a215845cbbd5e406a774b456cf137d5b to your computer and use it in GitHub Desktop.
This file contains 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
import psycopg2 | |
def execute_postgres(p_db_name, p_user, p_pass, p_query): | |
conn = None | |
try: | |
# connect to the PostgreSQL server | |
print('Attempting to connect...') | |
conn = psycopg2.connect("dbname=" + p_db_name + " user=" + p_user + " password=" + p_pass) | |
# create a cursor | |
cur = conn.cursor() | |
# execute a statement | |
cur.execute(p_query) | |
hole_by_hole_score = cur.fetchone() | |
print(hole_by_hole_score) | |
# close the communication with the PostgreSQL | |
cur.close() | |
except (Exception, psycopg2.DatabaseError) as error: | |
print(error) | |
finally: | |
if conn is not None: | |
conn.close() | |
print('DB Conn closed.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment