-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
39 lines (33 loc) · 1.01 KB
/
database.py
File metadata and controls
39 lines (33 loc) · 1.01 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
import psycopg2
import os
from dotenv import load_dotenv
# load .env
load_dotenv()
db_host = os.environ.get('DB_HOST')
db_dbname = os.environ.get('DB_DBNAME')
db_user = os.environ.get('DB_USER')
db_password = os.environ.get('DB_PASSWORD')
db_port = os.environ.get('DB_PORT')
class Databases:
def __init__(self):
self.db = psycopg2.connect(host=db_host, dbname=db_dbname, user=db_user,
password=db_password, port=db_port)
self.cursor = self.db.cursor()
print('DB 연결 성공')
def __del__(self):
self.db.close()
if self.cursor:
self.cursor.close()
def execute(self, query, args=None):
if args is None:
args = {}
try:
self.cursor.execute(query, args)
return self.cursor.fetchall()
except Exception as e:
print("Query 실행 에러: ", e)
return None
finally:
self.db.commit()
def commit(self):
self.db.commit()