-
Notifications
You must be signed in to change notification settings - Fork 1
/
connect_postgresql.py
40 lines (31 loc) · 1.02 KB
/
connect_postgresql.py
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
import psycopg2
from env import settings
pcon = settings.POSTGRESQL_CONFIG
class Databases():
def __init__(self) -> None:
self.db = psycopg2.connect(host=pcon['host'], dbname=pcon['db'],
user=pcon['user'], port=pcon['port'], password=pcon['password'])
self.cursor = self.db.cursor()
def __del__(self):
self.cursor.close()
self.db.close()
print('db 연결 종료')
def execute(self, query, args={}):
self.cursor.execute(query, args)
row = self.cursor.fetchall()
return row
def commit(self):
self.db.commit()
def insertDB(self, query):
try:
self.cursor.execute(query)
self.db.commit()
except Exception as e:
print(" insert DB err", e)
def readDB(self, query):
try:
self.cursor.execute(query)
result = self.cursor.fetchall()
except Exception as e:
result = (" read DB err", e)
return result