Skip to content

Instantly share code, notes, and snippets.

@gsweene2
Created November 12, 2018 02:27
Show Gist options
  • Save gsweene2/a215845cbbd5e406a774b456cf137d5b to your computer and use it in GitHub Desktop.
Save gsweene2/a215845cbbd5e406a774b456cf137d5b to your computer and use it in GitHub Desktop.
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