-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_connect.py
41 lines (33 loc) ยท 1.56 KB
/
db_connect.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
41
import pymysql
from dotenv import load_dotenv
load_dotenv()
import os
DB_PASSWORD=os.getenv('DB_PASSWORD')
# Mysql๊ณผ ์ฐ๊ฒฐ์ ์ค์ ํ๊ฑฐ๋ ํด์ ํฉ๋๋ค.
class ConnectDB:
# ์ธ์คํด์ค ์ด๊ธฐํ
def __init__(self, sql):
self.sql = sql # ์ธ์คํด์ค ๋ณ์ sql๊ฐ์ ์ค์ ํฉ๋๋ค.
self.data = None
# self.conn = pymysql.connect(host='localhost', user='root', password=DB_PASSWORD, db='Heydoctor', charset='utf8',
# autocommit=True) # DB์ ์ฐ๊ฒฐํฉ๋๋ค.
self.conn = pymysql.connect(host='db', user='heydoctor', password='password', db='HeyDoctor', charset='utf8',
autocommit=True) # DB์ ์ฐ๊ฒฐํฉ๋๋ค.
self.curs = self.conn.cursor(pymysql.cursors.DictCursor) # sql๋ฌธ ์ํ์ ์ํด cursor ๊ฐ์ฒด๋ฅผ ์์ฑํฉ๋๋ค.
def execute(self, *args):
self.curs.execute(self.sql, args)
self.conn.commit()
def execute_c(self, *args):
self.curs.execute(self.sql, *args)
self.conn.commit()
def fetch(self):
self.curs.execute(self.sql)
self.data = self.curs.fetchall()
return self.data
# ์ธ์คํด์ค ์ญ์
def __del__(self):
self.curs.close() # cursor ๊ฐ์ฒด๋ฅผ ๋ซ์ต๋๋ค.
self.conn.close() # DB์ฐ๊ฒฐ์ ํด์ ํฉ๋๋ค.
def connect():
return pymysql.connect(host='db', user='heydoctor', password='password', db='HeyDoctor', charset='utf8',
autocommit=True) # Connect to the MySQL database.