Advertisement
Guest User

Untitled

a guest
Aug 13th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. 1 #!/usr/bin/env python
  2. 2 #Connect to PosgreSQL and run a query
  3. 3 #By: Patrick Taylor
  4. 4
  5. 5
  6. 6 ##import modules
  7. 7
  8. 8 import psycopg2
  9. 9 import sys
  10. 10
  11. 11 ##connection values
  12. 12 database = 'postgres'
  13. 13 username = 'ptaylor'
  14. 14 hostname = 'localhost'
  15. 15 passwd = 'password'
  16. 16
  17. 17
  18. 18 ##try to connect or report a failure
  19. 19 def trying():
  20. 20 global database
  21. 21 global username
  22. 22 global hostname
  23. 23 global passwd
  24. 24 #database = 'postgres'
  25. 25 #username = 'ptaylor'
  26. 26 #hostname = '127.0.0.1'
  27. 27 #passwd = 'password'
  28. 28 try:
  29. 29 conn = psycopg2.connect("dbname=database user=username host=localhost password=passwd")
  30. 30 except:
  31. 31 print 'We cannot connect!'
  32. 32 sys.exit(1)
  33. 33
  34. 34
  35. 35 ##function to query database
  36. 36
  37. 37 def main():
  38. 38 global database
  39. 39 global username
  40. 40 global hostname
  41. 41 global passwd
  42. 42 #database = 'postgres'
  43. 43 #username = 'ptaylor'
  44. 44 #hostname = '127.0.0.1'
  45. 45 #passwd = 'password'
  46. 46
  47. 47 print database, username, hostname, passwd
  48. 48 conn = psycopg2.connect("dbname=database user=username host=hostname password=passwd")
  49. 49 cur = connect.cursor()
  50. 50 cur.execute("""select current_time""")
  51. 51 output = cur.fetchall()
  52. 52 return output
  53. 53
  54. 54 if __name__ == '__main__':
  55. 55 trying()
  56. 56 main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement