selectList
daoemp.py
import pymysql
class DaoEmp:
def __init__(self):
self.con = pymysql.connect(host='localhost', user='root', port=3305
,password='python', db='python', charset='utf8')
self.cur = self.con.cursor(pymysql.cursors.DictCursor)
def selectList(self):
sql = f'''
SELECT
e_id,
e_name,
gen,
addr
FROM emp
'''
self.cur.execute(sql)
list = self.cur.fetchall()
return list
def __del__(self):
self.cur.close()
self.con.close()
if __name__ == '__main__':
de= DaoEmp()
list = de.selectList()
print("list",list)
my_flask.py
de= DaoEmp() => 연결할 dao
list = de.selectList() => DaoEmp()에서selectList() 메소드 호출
return render_template('이동 URL',보낼 매개변수 = 보낼 매개변수)
from flask import Flask, request, render_template, redirect
from flask.templating import render_template
import pymysql
from day08.daoemp import DaoEmp
app = Flask(__name__)
@app.route('/')
@app.route('/emp_list')
def emp_list():
de=DaoEmp()
list = de.selectList()
return render_template('emp_list.html',list=list)
if __name__ == '__main__':
app.run(debug=True, port=88, host='0.0.0.0')
emp_list.html
location.href= "페이지 이동할 URL"
{{my_flask.py 받은 매개변수}}
list 안에 있는 튜플 지정은{{ .매개변수이름}} 사용
<!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>
emplist
{{list}}
<table border='1'>
<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>
selectOne
daoemp.py
import pymysql
class DaoEmp:
def __init__(self):
self.con = pymysql.connect(host='localhost', user='root', port=3305
,password='python', db='python', charset='utf8')
self.cur = self.con.cursor(pymysql.cursors.DictCursor)
def select(self,e_id):
sql = f'''
SELECT
e_id,
e_name,
gen,
addr
FROM emp
where
e_id= '{e_id}'
'''
self.cur.execute(sql)
vo = self.cur.fetchone()
return vo
def __del__(self):
self.cur.close()
self.con.close()
if __name__ == '__main__':
de= DaoEmp() #확인용
vo = de.select(1) #확인용
print("vo",vo) #확인용
my_flask.py
get 방식으로 파라미터 받음
e_id = request.args.get('e_id')
de=DaoEmp() => 연결할 dao
vo= de.select(파라미터) => DaoEmp()에서select(파라미터) 메소드 호출
return render_template('이동 URL',보낼 매개변수 = 보낼 매개변수)
https://lavender1122.tistory.com/139
from flask import Flask, request, render_template, redirect
from flask.templating import render_template
import pymysql
from day08.daoemp import DaoEmp
app = Flask(__name__)
@app.route('/emp_detail')
def emp_detail():
de=DaoEmp()
e_id = request.args.get('e_id')
vo = de.select(e_id)
return render_template('emp_detail.html',vo=vo)
if __name__ == '__main__':
app.run(debug=True, port=88, host='0.0.0.0')
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_del_act(){
var flag=confirm("한 분 지워진 데이터는 복구불가합니다.\n 그래도 삭제 하시겠습니까?");
if(!flag){
return; //밑으로 못내려가게 맞는다
}
document.frm.submit();
}
</script>
</head>
<body>
EMP DETAIL<br>
{{vo}}
<form name="frm" action="emp_del" method="post">
<table border='1'>
<tr>
<th>e_id</th>
<td>{{vo.e_id}}</td>
<input type ="hidden" name =e_id value="{{vo.e_id}}">
</tr>
<tr>
<th>e_name</th>
<td>{{vo.e_name}}</td>
</tr>
<tr>
<th>gen</th>
<td>{{vo.gen}}</td>
</tr>
<tr>
<th>addr</th>
<td>{{vo.addr}}</td>
</tr>
<tr>
<td colspan="2">
<input type = "button" value ="수정" onclick="fn_mod()">
<input type = "button" value ="삭제" onclick="fn_del_act()">
</td>
</tr>
</table>
</form>
</body>
</html>
insert
daoemp.py
import pymysql
class DaoEmp:
def __init__(self):
self.con = pymysql.connect(host='localhost', user='root', port=3305
,password='python', db='python', charset='utf8')
self.cur = self.con.cursor(pymysql.cursors.DictCursor)
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}')
'''
cnt = self.cur.execute(sql)
self.con.commit()
return cnt
def __del__(self):
self.con.commit()
self.cur.close()
self.con.close()
if __name__ == '__main__':
de= DaoEmp() # 확인용
cnt = de.insert('3','3','3','3')# 확인용
print("cnt",cnt) #성공 1, 실패 0 확인용
my_flask.py
from flask import Flask, request, render_template, redirect
from flask.templating import render_template
import pymysql
from day08.daoemp import DaoEmp
app = Flask(__name__)
@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)
if __name__ == '__main__':
app.run(debug=True, port=88, host='0.0.0.0')
emp_add.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function fn_add_act(){
document.frm.submit();
}
</script>
</head>
<body>
EMP_ADD HTML <br>
<form name="frm" action="emp_add_act" method="post">
<table border='1'>
<tr>
<th>사번</th>
<td>
<input type="type" name ="e_id" value="">
</td>
</tr>
<tr>
<th>이름</th>
<td>
<input type="type" name ="e_name" value="">
</td>
</tr>
<tr>
<th>gen</th>
<td>
<input type="type" name ="gen" value="">
</td>
</tr>
<tr>
<th>addr</th>
<td>
<input type="type" name ="addr" value="">
</td>
</tr>
<tr>
<td colspan="2">
<input type = "button" value ="저장" onclick = "fn_add_act()">
</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}};
if(cnt >0){
alert("정상적으로 추가 되었습니다.");
location.href="emp_list";
}else{
alert("추가 도중 문제가 생겼습니다.")
history.back();// 추가 페이지로 이동
}
</script>
</head>
<body>
EMP ADD ACT HTML <br>
{{cnt}}
</body>
</html>
update
daoemp.py
import pymysql
class DaoEmp:
def __init__(self):
self.con = pymysql.connect(host='localhost', user='root', port=3305
,password='python', db='python', charset='utf8')
self.cur = self.con.cursor(pymysql.cursors.DictCursor)
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}'
'''
cnt = self.cur.execute(sql)
self.con.commit()
return cnt
def __del__(self):
self.cur.close()
self.con.close()
if __name__ == '__main__':
de= DaoEmp()
# list = de.selectList()
cnt = de.update('3','6','6','6')
print("cnt",cnt) #성공1, 실패0
my_flask.py
from flask import Flask, request, render_template, redirect
from flask.templating import render_template
import pymysql
from day08.daoemp import DaoEmp
app = Flask(__name__)
@app.route('/emp_mod')
def emp_mod():
de=DaoEmp()
e_id = request.args.get('e_id',"null")
vo = de.select(e_id)
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)
if __name__ == '__main__':
app.run(debug=True, port=88, host='0.0.0.0')
emp_mod.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function fn_mod_act(){
document.frm.submit();
}
</script>
</head>
<style>
table{ border-collapse : collapse; } /*이중선 제거*/
th,td{
width: 100px;
height: 50px;
text-align: center;
border: 1px solid #000;
vertical-align: top; /* 위 */
vertical-align: bottom; /* 아래 */
vertical-align: middle; /* 가운데 */
}
</style>
<body>
EMP mod <br>
<form name="frm" action="emp_mod_act" method="post">
<table border='1'>
<tr>
<th>사번</th>
<td>
<input type="type" name ="e_id" value="{{vo.e_id}}">
</td>
</tr>
<tr>
<th>이름</th>
<td>
<input type="type" name ="e_name" value="{{vo.e_name}}">
</td>
</tr>
<tr>
<th>gen</th>
<td>
<input type="type" name ="gen" value="{{vo.gen}}">
</td>
</tr>
<tr>
<th>addr</th>
<td>
<input type="type" name ="addr" value="{{vo.addr}}">
</td>
</tr>
<tr>
<td colspan="2">
<input type = "button" value ="저장" onclick = "fn_mod_act()">
</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}}; //에러 아님
if(cnt>0){
alert("정상적으로 수정되었습니다.");
location.href="emp_list";
}else{
alert("수정도중 문제가 생겼습니다.");
history.back(); //수정한 페이지로 이동 (파라미터도 같음)
}
</script>
</head>
<body>
EMP_MOD_ACT HTML <br>
{{cnt}}
</body>
</html>
delete
daoemp.py
import pymysql
class DaoEmp:
def __init__(self):
self.con = pymysql.connect(host='localhost', user='root', port=3305
,password='python', db='python', charset='utf8')
self.cur = self.con.cursor(pymysql.cursors.DictCursor)
def delete(self,e_id):
sql = f'''
delete from emp
where
e_id='{e_id}'
'''
cnt = self.cur.execute(sql)
self.con.commit()
return cnt
def __del__(self):
self.cur.close()
self.con.close()
if __name__ == '__main__':
de= DaoEmp()
# list = de.selectList()
cnt = de.delete('3')
print("cnt",cnt)
my_flask.py
from flask import Flask, request, render_template, redirect
from flask.templating import render_template
import pymysql
from day08.daoemp import DaoEmp
app = Flask(__name__)
@app.route('/emp_del',methods=['POST'])
def emp_del():
e_id = request.form['e_id']
de=DaoEmp()
cnt = de.delete(e_id)
return render_template('emp_del_act.html',cnt=cnt)
if __name__ == '__main__':
app.run(debug=True, port=88, host='0.0.0.0')
emp_del_act.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
var cnt = {{cnt}}; //에러 아님
if(cnt>0){
alert("정상적으로 삭제었습니다.");
location.href="emp_list";
}else{
alert("삭제도중 문제가 생겼습니다.");
history.back(); //수정한 페이지로 이동 (파라미터도 같음)
}
</script>
</head>
<body>
EMP DEL ACT <br>
{{cnt}}
</body>
</html>
본문
daoemp.py
import pymysql
class DaoEmp:
def __init__(self):
self.con = pymysql.connect(host='localhost', user='root', port=3305
,password='python', db='python', charset='utf8')
self.cur = self.con.cursor(pymysql.cursors.DictCursor)
def selectList(self):
sql = f'''
SELECT
e_id,
e_name,
gen,
addr
FROM emp
'''
self.cur.execute(sql)
list = self.cur.fetchall()
return list
def select(self,e_id):
sql = f'''
SELECT
e_id,
e_name,
gen,
addr
FROM emp
where
e_id= '{e_id}'
'''
self.cur.execute(sql)
vo = self.cur.fetchone()
return vo
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}')
'''
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}'
'''
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}'
'''
cnt = self.cur.execute(sql)
self.con.commit()
return cnt
def __del__(self):
self.cur.close()
self.con.close()
if __name__ == '__main__':
de= DaoEmp()
# vo = de.select(1) #확인용
# cnt = de.delete(3)#확인용
# print("cnt",cnt)#확인용
# print("vo",vo)#확인용
my_flask.py
from flask import Flask, request, render_template, redirect
from flask.templating import render_template
import pymysql
from day08.daoemp import DaoEmp
app = Flask(__name__)
@app.route('/')
@app.route('/emp_list')
def emp_list():
de=DaoEmp()
list = de.selectList()
return render_template('emp_list.html',list=list)
@app.route('/emp_detail')
def emp_detail():
de=DaoEmp()
e_id = request.args.get('e_id')
vo = de.select(e_id)
return render_template('emp_detail.html',vo=vo)
@app.route('/emp_mod')
def emp_mod():
de=DaoEmp()
e_id = request.args.get('e_id',"null")
vo = de.select(e_id)
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_del',methods=['POST'])
def emp_del():
e_id = request.form['e_id']
de=DaoEmp()
cnt = de.delete(e_id)
return render_template('emp_del_act.html',cnt=cnt)
if __name__ == '__main__':
app.run(debug=True, port=88, host='0.0.0.0')
'Python > flask' 카테고리의 다른 글
axios 이용한 CRUD (0) | 2024.04.10 |
---|---|
axios (0) | 2024.04.10 |
flask Insert,Update, Delete 문 (0) | 2024.04.03 |
render_template (0) | 2024.04.03 |
flask get방식, post 방식 파라미터 넣기 (0) | 2024.04.03 |