-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
41 lines (34 loc) · 1.05 KB
/
database.py
File metadata and controls
41 lines (34 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import psycopg2
from psycopg2 import Error
class PostgresDB:
def __init__(self, user, password, host, port, database):
self.user = user
self.password = password
self.host = host
self.port = port
self.database = database
self.connection = None
def connect(self):
try:
self.connection = psycopg2.connect(
user=self.user,
password=self.password,
host=self.host,
port=self.port,
database=self.database
)
except Error as error:
print("Ошибка при подключении к PostgreSQL:", error)
raise
def commit(self):
if self.connection:
self.connection.commit()
def disconnect(self):
if self.connection:
self.connection.close()
self.connection = None
def __enter__(self):
self.connect()
return self
def __exit__(self, exc_type, exc_value, traceback):
self.disconnect()