-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmysql_connection.py
More file actions
55 lines (45 loc) Β· 1.76 KB
/
mysql_connection.py
File metadata and controls
55 lines (45 loc) Β· 1.76 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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
CodeCraft PMS Project
νμΌλͺ
: mysql_connection.py
μμ±μ : κΉμ°½ν
μμ±μΌ : 2024/10/14
μ
λ°μ΄νΈ : 2024/11/10
μ€λͺ
: APIμ DBλ₯Ό μ°κ²°νλ κΈ°λ₯ μ μ
"""
import pymysql
from dotenv import load_dotenv
import os
# MySQL μλ²μ λ‘κ·ΈμΈνκ³ PMS DBμ μ μνλ ν¨μ
def db_connect():
load_dotenv()
password = os.getenv('DB_PASSWORD')
connection = pymysql.connect(
host='192.168.50.84',
user='root',
password=password,
charset='utf8mb4',
database='PMS'
)
return connection
# MySQL μλ²μ λ‘κ·ΈμΈνμ¬ μ μνλ ν¨μ
# DML μμ
μ μνν DB λμμ μ§μ νμ§ μμΌλ©°, ν
μ΄λΈμ΄ μλ DB μ체λ₯Ό μμ±νκ±°λ μμ νλ λ±μ κ²½μ°μ μ¬μ©νλ€
def db_root_connect():
load_dotenv()
password = os.getenv('DB_PASSWORD')
connection = pymysql.connect(
host='192.168.50.84',
user='root',
password=password,
charset='utf8mb4'
)
return connection
# db_session = db_connect()
# try: # DBμ μ μν΄ ν
μ΄λΈ 리μ€νΈ μΆλ ₯
# with db_session.cursor() as cursor:
# cursor.execute("SHOW DATABASES")
# databases = cursor.fetchall()
# print("Databases:")
# for db in databases:
# print(db[0])
# finally:
# db_session.close()