Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import psycopg2
- class PostgreSQLConnection:
- def __init__(self, host, port, database, user, password):
- self.host = host
- self.port = port
- self.database = database
- self.user = user
- self.password = password
- self.connection = None
- def connect(self):
- if self.connection is None:
- self.connection = psycopg2.connect(
- host=self.host,
- port=self.port,
- database=self.database,
- user=self.user,
- password=self.password
- )
- def close(self):
- if self.connection:
- self.connection.close()
- self.connection = None
- def __enter__(self):
- self.connect()
- return self.connection
- def __exit__(self, exc_type, exc_val, exc_tb):
- self.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement