DAO
import pymysql
class DaoEmp:
def __init__(self):
self.con = pymysql.connect(host='127.0.0.1', port= 3305, user='root', password='python',
db='python', charset='utf8')
self.cur = self.con.cursor(pymysql.cursors.DictCursor)
def selectList(self):
sql = "SELECT * FROM emp"
self.cur.execute(sql)
list = self.cur.fetchall()
return list
def selectOne(self,e_id):
sql = f"""
SELECT
e_id,e_name,gen,addr
FROM
emp
WHERE
e_id = '{e_id}'
"""
print(sql)
self.cur.execute(sql)
vo = self.cur.fetchone()
return vo
def __del__(self):
self.cur.close()
self.con.close()
def insert(self,e_id,e_name,gen,addr):
sql = f"""
INSERT INTO emp
(e_id,e_name,gen,addr)
VALUES
('{e_id}','{e_name}','{gen}','{addr}')
"""
print(sql)
cnt = self.cur.execute(sql)
self.con.commit()
return cnt
def update(self,e_id,e_name,gen,addr):
sql = f"""
UPDATE emp
SET
e_name='{e_name}',
gen='{gen}',
addr='{addr}'
WHERE
e_id = '{e_id}'
"""
print(sql)
cnt = self.cur.execute(sql)
self.con.commit()
return cnt
def delete(self,e_id):
sql = f"""
DELETE FROM emp
WHERE
e_id='{e_id}'
"""
print(sql)
cnt = self.cur.execute(sql)
self.con.commit()
return cnt
# if __name__ == '__main__':
# de = DaoEmp()
# vo = de.selectOne('1')
# print("vo",vo)
if __name__ == '__main__':
de = DaoEmp()
cnt = de.insert('4','4','4','4')
print("cnt",cnt)
myflask2.py
import pymysql
from flask import Flask
from flask.globals import request
from flask.templating import render_template
from day09.daoemp import DaoEmp
app = Flask(__name__)
@app.route('/')
@app.route('/emp_list')
def main():
# dao 생성 및 호출
de = DaoEmp()
list = de.selectList()
return render_template('emp_list.html',list=list)
@app.route('/emp_detail') # GET 방식
def emp_detail():
e_id = request.args['e_id']
de = DaoEmp()
vo= de.selectOne(e_id)
print(vo)
return render_template('emp_detail.html',vo=vo)
@app.route('/emp_mod')
def emp_mod():
e_id = request.args['e_id']
de = DaoEmp()
vo= de.selectOne(e_id)
print(vo)
return render_template('emp_mod.html',vo=vo)
@app.route('/emp_mod_act', methods=['POST'])
def emp_mod_act():
e_id = request.form['e_id']
e_name = request.form['e_name']
gen = request.form['gen']
addr = request.form['addr']
de = DaoEmp()
cnt = de.update(e_id, e_name, gen, addr)
return render_template('emp_mod_act.html',cnt=cnt)
@app.route('/emp_add')
def emp_add():
return render_template('emp_add.html')
@app.route('/emp_add_act', methods=['POST'])
def emp_add_act():
e_id = request.form['e_id']
e_name = request.form['e_name']
gen = request.form['gen']
addr = request.form['addr']
de = DaoEmp()
cnt = de.insert(e_id, e_name, gen, addr)
return render_template('emp_add_act.html',cnt=cnt)
@app.route('/emp_delete_act', methods=['POST'])
def emp_delete_act():
e_id = request.form['e_id']
de = DaoEmp()
cnt= de.delete(e_id)
return render_template('emp_delete_act.html',cnt=cnt)
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=True, port=80)
HTML
더보기
더보기
emp_list.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function fn_add(){
location.href = "emp_add"
}
</script>
</head>
<body>
EMP LIST 화면
<table border="1px">
<tr>
<th>사번</th>
<th>이름</th>
<th>성별</th>
<th>주소</th>
</tr>
{% for e in list %}
<tr>
<td><a href="/emp_detail?e_id={{e.e_id}}">{{e.e_id}}</a></td>
<td>{{e.e_name}}</td>
<td>{{e.gen}}</td>
<td>{{e.addr}}</td>
</tr>
{% endfor %}
</table>
<input type="button" value="추가" onclick="fn_add()">
</body>
</html>
emp_add.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function fn_add(){
document.frm.submit();
}
</script>
</head>
<body>
EMP ADD 화면
<form action="emp_add_act" name="frm" method="post">
<table border="1px">
<tr>
<td>사번</td>
<td><input type="text" name="e_id" /></td>
</tr>
<tr>
<td>이름</td>
<td><input type="text" name="e_name" /></td>
</tr>
<tr>
<td>성별</td>
<td><input type="text" name="gen" /></td>
</tr>
<tr>
<td>주소</td>
<td><input type="text" name="addr" /></td>
</tr>
<tr>
<td colspan="2">
<input type="button" value="저장" onclick="fn_add()" />
</td>
</tr>
</table>
</form>
</body>
</html>
emp_add_act.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
var cnt = {{cnt}};
console.log("cnt",cnt);
if(cnt == 1){
alert("정상적으로 추가되었습니다.");
location.href="emp_list";
}else{
alert("추가도중 문제가 생겼습니다.");
history.back(); // 문제가 생겼을 때 이전 페이지로 가기
}
</script>
</head>
<body>
EMP ADD ACT 화면
</body>
</html>
emp_detail.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function fn_mod(){
location.href="emp_mod?e_id={{vo.e_id}}"
}
function fn_delete(){
document.frm.submit();
}
</script>
</head>
<body>
EMP DETAIL 화면
<table border="1">
<tr>
<td>사번</td>
<td>{{vo.e_id}}</td>
</tr>
<tr>
<td>이름</td>
<td>{{vo.e_name}}</td>
</tr>
<tr>
<td>성별</td>
<td>{{vo.gen}}</td>
</tr>
<tr>
<td>주소</td>
<td>{{vo.addr}}</td>
</tr>
<tr>
<td colspan="2">
<input type="button" value="수정" onclick="fn_mod()"/>
<form action="emp_delete_act" name="frm" method="post">
<input type="button" value="삭제" onclick="fn_delete()"/>
<input type="hidden" value="{{vo.e_id}}" name="e_id"/>
</form>
</td>
</tr>
</table>
</body>
</html>
emp_delete_act.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
var cnt = {{cnt}};
console.log("cnt",cnt);
if(cnt == 1){
alert("정상적으로 삭제되었습니다.");
location.href="emp_list";
}else{
alert("삭제도중 문제가 생겼습니다.");
history.back(); // 문제가 생겼을 때 이전 페이지로 가기
}
</script>
</head>
<body>
EMP DELETE ACT 화면
</body>
</html>
emp_mod.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function fn_mod(){
document.frm.submit();
}
</script>
</head>
<body>
EMP MOD 화면
<form action="emp_mod_act" name="frm" method="post">
<table border="1px">
<tr>
<td>사번</td>
<td><input type="text" name="e_id" value="{{vo.e_id}}" /></td>
</tr>
<tr>
<td>이름</td>
<td><input type="text" name="e_name" value="{{vo.e_name}}" /></td>
</tr>
<tr>
<td>성별</td>
<td><input type="text" name="gen" value="{{vo.gen}}" /></td>
</tr>
<tr>
<td>주소</td>
<td><input type="text" name="addr" value="{{vo.addr}}" /></td>
</tr>
<tr>
<td colspan="2">
<input type="button" value="저장" onclick="fn_mod()" />
</td>
</tr>
</table>
</form>
</body>
</html>
emp_mod_act.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
var cnt = {{cnt}};
console.log("cnt",cnt);
if(cnt == 1){
alert("정상적으로 수정되었습니다.");
location.href="emp_list";
}else{
alert("수정도중 문제가 생겼습니다.");
history.back(); // 문제가 생겼을 때 이전 페이지로 가기
}
</script>
</head>
<body>
EMP MOD ACT 화면
</body>
</html>
'파이썬 > 수업내용' 카테고리의 다른 글
[Python] jquery (0) | 2024.07.09 |
---|---|
[Python] web외 연동하여 로또 생성하기 / 구구단 / 가위바위보 / 전회번호 출력하기 / 야구게임 (0) | 2024.07.08 |
[Python] Flask DB 연동하기 (0) | 2024.07.05 |
[python] Flask (0) | 2024.07.04 |
[Python] Web Progeanming (0) | 2024.07.03 |