Skip to content

Commit

Permalink
[feat] SQL alchemy 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
rover0811 committed Dec 24, 2022
1 parent c14a60a commit 9e3acf9
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
90 changes: 90 additions & 0 deletions API/insert_sql.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, String, Float, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import Session
from sqlalchemy.exc import IntegrityError
import datetime

Base = declarative_base()

def get_score():
"""
성적 데이터를 토대로 점수를 계산한다.
"""


class User(Base):
"""
ORM활용을 위한 테이블 객체를 정의합니다.
"""
__tablename__ = "user" #사용할 테이블 이름

studentId = Column(Integer, primary_key=True)
phl = Column(Integer)
math = Column(Integer)
bigData = Column(Integer)
programming = Column(Integer)
businessManagement= Column(Integer)
department = Column(String)
com_score = Column(Float)
soft_score = Column(Float)
updateDate = Column(DateTime)


def __init__(self, student_id, phl, math, big_data, programming, business_management, department, com_score, soft_score, update_date):
self.studentId = student_id
self.phl = phl
self.math = math
self.bigData = big_data
self.programming = programming
self.businessManagement = business_management
self.department = department
self.com_score = com_score
self.soft_score = soft_score
self.updateDate = update_date


def insert_data_db(insert_data:dict):
"""
DB 유저 테이블에 데이터를 삽입합니다.
Args:
student_id (int): 학번
phl (int): 성적
math (int): 성적
big_data (int): 성적
programming (int): 성적
business_management (int): 성적
department (int): 성적
score (int): 총합점수
update_date (datetime): 최신 업데이트 일수
Returns:
_type_: _description_
"""
#gcp 유저와 비밀번호
engine = create_engine(f"mysql+pymysql://root:[email protected]:3306/grade", encoding='utf-8')
# user = User(**insert_data)
session = Session(engine) #세션을 생성
a=session.query(User).filter(User.studentId==f"{insert_data['student_id']}").update({User.bigData:insert_data["big_data"],User.businessManagement:insert_data["business_management"],User.math:insert_data["math"],User.phl:insert_data["phl"],User.programming:insert_data["programming"],User.updateDate:insert_data["update_date"],User.com_score:insert_data["com_score"],User.soft_score:insert_data["soft_score"]})
try:
session.commit() #SQL 디비에 전송
session.close()
return True #DB 삽입이 문제 없이 종료

except TimeoutError as e: #DB 연결을 위한 시간이 너무 오래 걸리는 경우
print("Exception in insert data to DB", e)

except IntegrityError as e: #기본 키 등의 제약이 지켜지지 않은 경우
print("Integirity Violence Errror!", e)



# if __name__ == "__main__":

# now = datetime.datetime.now() #현재시각

# #더미 데이터 입니다.
# insert_data = {'student_id':20191305, 'phl':90, 'math':90, 'big_data':90, 'programming':90, 'business_management':90,
# 'department':"컴", 'com_score':0, 'soft_score':0, 'update_date':now}
# user = insert_data_db(insert_data)
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ pydantic
selenium
uvicorn
fastapi
webdriver_manager
webdriver_manager
sqlalchemy
pymysql

0 comments on commit 9e3acf9

Please sign in to comment.