Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- from datetime import datetime
- from random import randint
- from time import sleep
- import psycopg2
- def main():
- try:
- connection = psycopg2.connect(user = "admin",
- password = "P@ssw0rd",
- host = "89.208.87.38",
- port = "5432",
- database = "myproddb")
- cursor = connection.cursor()
- cursor.execute("SELECT version();")
- record = cursor.fetchone()
- print("Connection opened to", record[0])
- cursor.execute(
- "INSERT INTO log VALUES ({});".format(randint(1, 10000)))
- connection.commit()
- cursor.execute("SELECT COUNT(event_id) from log;")
- record = cursor.fetchone()
- print("Logged a value, overall count: {}".format(record[0]))
- except Exception as error:
- print ("Error while connecting to PostgreSQL", error)
- finally:
- if connection:
- cursor.close()
- connection.close()
- print("Connection closed")
- if __name__ == '__main__':
- try:
- while True:
- try:
- print(datetime.now())
- main()
- sleep(3)
- except Exception as e:
- print("Caught error:\n", e)
- sleep(1)
- except KeyboardInterrupt:
- print("exit")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement