Guest User

Untitled

a guest
Nov 11th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. import psycopg2
  2.  
  3. def execute_postgres(p_db_name, p_user, p_pass, p_query):
  4. conn = None
  5. try:
  6.  
  7. # connect to the PostgreSQL server
  8. print('Attempting to connect...')
  9. conn = psycopg2.connect("dbname=" + p_db_name + " user=" + p_user + " password=" + p_pass)
  10.  
  11. # create a cursor
  12. cur = conn.cursor()
  13.  
  14. # execute a statement
  15. cur.execute(p_query)
  16. hole_by_hole_score = cur.fetchone()
  17. print(hole_by_hole_score)
  18.  
  19. # close the communication with the PostgreSQL
  20. cur.close()
  21. except (Exception, psycopg2.DatabaseError) as error:
  22. print(error)
  23. finally:
  24. if conn is not None:
  25. conn.close()
  26. print('DB Conn closed.')
Add Comment
Please, Sign In to add comment