From 9e3acf96c4318462386f7689de88e6a96ca605e1 Mon Sep 17 00:00:00 2001 From: rover0811 Date: Sat, 24 Dec 2022 23:46:33 +0900 Subject: [PATCH] =?UTF-8?q?[feat]=20SQL=20alchemy=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API/insert_sql.py | 90 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 4 ++- 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 API/insert_sql.py diff --git a/API/insert_sql.py b/API/insert_sql.py new file mode 100644 index 0000000..f9ba867 --- /dev/null +++ b/API/insert_sql.py @@ -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:0000@34.64.94.74: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) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 27a240e..8136b9e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,6 @@ pydantic selenium uvicorn fastapi -webdriver_manager \ No newline at end of file +webdriver_manager +sqlalchemy +pymysql \ No newline at end of file