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