Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.')
Add Comment
Please, Sign In to add comment