Data_Analysis_Track_33/Python_문제풀이
Python_07_문제풀이(pymysql TODO 문제)
lsc99
2023. 9. 11. 19:33
TODO
다음 함수들을 구현하시오.
각 함수들은 member 테이블과 관련해서 CRUD를 처리하는 함수들이다.
1. name, email, tall, birthday를 매개변수로 받아서 insert하는 함수. (id는 자동증가, created_at은 실행시점의 일시가 insert되도록 한다.)
2. id, name, email, tall, birthday를 매개변수로 받아서 id의 member의 나머지 정보를 update하는 함수. (created_at은 update하지 않는다.)
3. id를 매개변수로 받아서 그 member 를 삭제하는 함수.
4. 이름을 매개변수로 받아서 그 이름의 member를 삭제하는 함수.
5. id를 매개변수로 받아서 그 id의 회원 정보를 조회하여 반환하는 함수.
6. name을 매개변수로 받아서 그 이름이 들어간 회원의 정보를 조회하여 반환하는 함수.
7. birthday를 매개변수로 받아서 그 생일의 회원의 정보를 조회하여 반환하는 함수.
8. tall 값을 두개 받아서 그 범위의 tall인 회원들의 정보를 조회하여 반환하는 함수.
insert, update, delete 는 적용된 행의 개수를 반환한다.
select 처리 함수는 조회결과를 반환한다.
import os
os.makedirs('dao', exist_ok=True) # dao 디렉토리 생성
# dao 모듈 - Data Access Object : sql문을 실행하는 함수/메소드들로 구성된 모듈.
# %%writefile dao/member_dao.py
import pymysql
from datetime import date, datetime
def insert_member(name:str, email:str, tall:float, birthday:date) -> int:
"""
회원정보를 insert하는 경우
[parameter]
[return]
int: insert한 행수
[exception]
"""
sql = "insert into member (name, email, tall, birthday, created_at) \
values (%s, %s, %s, %s, now())"
datas = (name, email, tall, birthday)
with pymysql.connect(host = 'localhost', port = 3306, user = 'playdata', password = '1111', db = 'testdb') as conn:
with conn.cursor() as cursor:
cnt = cursor.execute(sql, datas)
# print(cnt)
conn.commit()
return cnt
def update_member(id, name, email, tall, birthday):
"""
ID의 회원의 name, email, tall, birthday를 update
[return]
int : update된 행수
"""
sql = "update member set name=%s, email=%s, tall=%s, birthday=%s where id = %s"
datas = (name, email, tall, birthday, id)
with pymysql.connect(host = 'localhost', port = 3306, user = 'playdata', password = '1111', db = 'testdb') as conn:
with conn.cursor() as cursor:
cnt = cursor.execute(sql, datas)
# print(cnt)
conn.commit()
return cnt
def delete_member_by_id(id):
"""
ID의 회원정보를 delete
[return]
int : 삭제한 행수
"""
sql = "delete from member where id = %s"
with pymysql.connect(host = 'localhost', port = 3306, user = 'playdata', password = '1111', db = 'testdb') as conn:
with conn.cursor() as cursor:
cnt = cursor.execute(sql, id)
# print(cnt)
conn.commit()
return cnt
def delete_member_by_name(name):
"""
이름으로 회원정보 삭제
[return]
int : 삭제한 행수
"""
sql = "delete from member where name = %s"
with pymysql.connect(host = 'localhost', port = 3306, user = 'playdata', password = '1111', db = 'testdb') as conn:
with conn.cursor() as cursor:
cnt = cursor.execute(sql, name)
conn.commit()
return cnt
def select_member_by_id(id):
"""
id로 회원정보 조회
[return]
tuple : 조회한 결과
"""
sql = "select * from member where id = %s"
with pymysql.connect(host = 'localhost', port = 3306, user = 'playdata', password = '1111', db = 'testdb') as conn:
with conn.cursor() as cursor:
cursor.execute(sql, id)
# pk 조회 -> 결과행 : 0,1
return cursor.fetchone() # 1행: tuple, 0행: None
def select_member_by_name(name):
"""
이름으로 회원정보 조회
[return]
tuple: 조회한 결과
"""
# sql = "select * from member where name = %s"
sql = "select * from member where name like %s"
name = f'%{name}%'
print(name)
with pymysql.connect(host="localhost", port=3306, user='root', password="1111", db='testdb') as conn:
with conn.cursor() as cursor:
cursor.execute(sql, name)
return cursor.fetchall()
def select_member_by_birthday(birthday=None):
"""
생일로 회원정보 조회
[parameter]
birthday : date - 조회할 생일. None : 생일이 null인 행을 조회
[return]
tuple : 조회한 결과
"""
sql = "select * from member where birthday {}"
if birthday is None: # null인 것 조회
sql = sql.format("is null")
else:
sql = sql.format("= %s")
print(sql)
with pymysql.connect(host = 'localhost', port = 3306, user = 'playdata', password = '1111', db = 'testdb') as conn:
with conn.cursor() as cursor:
if birthday is None:
cursor.execute(sql) # birthday is null
else:
cursor.execute(sql, birthday) # birthday = %s
return cursor.fetchall()
def select_member_by_tall_range(start_tall, end_tall):
"""
키의 범위로 회원정보 조회
[parameter]
start_tall : float
end_tall : float
start_tall <= tall <= end_tall 범위의 회원을 조회.
"""
sql = "select * from member where tall between %s and %s"
with pymysql.connect(host = 'localhost', port = 3306, user = 'playdata', password = '1111', db = 'testdb') as conn:
with conn.cursor() as cursor:
cnt = cursor.execute(sql, (start_tall, end_tall))
return cursor.fetchall()
def select_all_member():
"""
전체 회원정보를 조회
"""
sql = "select * from member"
with pymysql.connect(host = 'localhost', port = 3306, user = 'playdata', password = '1111', db = 'testdb') as conn:
with conn.cursor() as cursor:
cursor.execute(sql)
return cursor.fetchall()
결과확인
1번
cnt = insert_member('유관순', 'y@3.com', 155.23, date(1960,8,11))
print(cnt)
# 실행하고 MySQL Workbench에서 결과값 확인
2번
cnt = update_member(13, '아이아', 'a@2347.com', 165.21, date(1999, 10, 22))
print(cnt)
3번
cnt = delete_member_by_id(6)
print(cnt)
4번
cnt = delete_member_by_name('name2')
print(cnt)
5번
cnt = select_member_by_id(20)
print(cnt)
6번
select_member_by_name("유") # 이름에 '유'가 들어가는 사람들 조회
7번
select_member_by_birthday('1960-08-11')
select_member_by_birthday() # null값
8번
select_member_by_tall_range(180, 190)
전체 회원정보 조회
select_all_member()