实现一个tornado通过mysql向layui表格post数据的的例子,要有html实现的代码
时间: 2023-05-14 15:05:50 浏览: 172
以下是一个简单的示例代码,可以实现tornado通过mysql向layui表格post数据:
HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Layui表格数据提交示例</title>
<link rel="stylesheet" href="https://cdn.staticfile.org/layui/2.5.6/css/layui.min.css">
</head>
<body>
<div class="layui-container">
<div class="layui-row">
<div class="layui-col-md12">
<table class="layui-hide" id="demo" lay-filter="test"></table>
</div>
</div>
</div>
<script src="https://cdn.staticfile.org/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/layui/2.5.6/layui.min.js"></script>
<script>
layui.use(['table', 'layer'], function(){
var table = layui.table;
var layer = layui.layer;
//渲染表格
table.render({
elem: '#demo',
url: '/get_data',
cols: [[
{field: 'id', title: 'ID', width: 80},
{field: 'name', title: '姓名', width: 120},
{field: 'age', title: '年龄', width: 80},
{field: 'email', title: '邮箱', width: 200},
{field: 'phone', title: '电话', width: 120},
{field: 'address', title: '地址', width: 200},
{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
]],
page: true
});
//监听工具条
table.on('tool(test)', function(obj){
var data = obj.data;
if(obj.event === 'del'){
layer.confirm('真的删除行么', function(index){
$.ajax({
url: '/delete_data',
type: 'POST',
data: {id: data.id},
success: function(res){
if(res.code === 0){
obj.del();
layer.close(index);
}else{
layer.msg(res.msg);
}
},
error: function(){
layer.msg('请求失败');
}
});
});
} else if(obj.event === 'edit'){
layer.prompt({
formType: 2,
value: data.email
}, function(value, index){
$.ajax({
url: '/update_data',
type: 'POST',
data: {id: data.id, email: value},
success: function(res){
if(res.code === 0){
obj.update({
email: value
});
layer.close(index);
}else{
layer.msg(res.msg);
}
},
error: function(){
layer.msg('请求失败');
}
});
});
}
});
});
</script>
<!-- 表格操作栏 -->
<script type="text/html" id="barDemo">
<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
</body>
</html>
```
Python代码:
```python
import tornado.ioloop
import tornado.web
import tornado.escape
import pymysql.cursors
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html")
class GetDataHandler(tornado.web.RequestHandler):
def get(self):
# 连接数据库
connection = pymysql.connect(host='localhost',
user='root',
password='123456',
db='test',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
# 查询数据
sql = "SELECT * FROM `user`"
cursor.execute(sql)
result = cursor.fetchall()
self.write(tornado.escape.json_encode({'code': 0, 'msg': '', 'count': len(result), 'data': result}))
except:
self.write(tornado.escape.json_encode({'code': 1, 'msg': '查询数据失败'}))
finally:
connection.close()
def post(self):
# 连接数据库
connection = pymysql.connect(host='localhost',
user='root',
password='123456',
db='test',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
# 插入数据
sql = "INSERT INTO `user` (`name`, `age`, `email`, `phone`, `address`) VALUES (%s, %s, %s, %s, %s)"
cursor.execute(sql, (self.get_argument('name'), self.get_argument('age'), self.get_argument('email'), self.get_argument('phone'), self.get_argument('address')))
connection.commit()
self.write(tornado.escape.json_encode({'code': 0, 'msg': '插入数据成功'}))
except:
self.write(tornado.escape.json_encode({'code': 1, 'msg': '插入数据失败'}))
finally:
connection.close()
class UpdateDataHandler(tornado.web.RequestHandler):
def post(self):
# 连接数据库
connection = pymysql.connect(host='localhost',
user='root',
password='123456',
db='test',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
# 更新数据
sql = "UPDATE `user` SET `email`=%s WHERE `id`=%s"
cursor.execute(sql, (self.get_argument('email'), self.get_argument('id')))
connection.commit()
self.write(tornado.escape.json_encode({'code': 0, 'msg': '更新数据成功'}))
except:
self.write(tornado.escape.json_encode({'code': 1, 'msg': '更新数据失败'}))
finally:
connection.close()
class DeleteDataHandler(tornado.web.RequestHandler):
def post(self):
# 连接数据库
connection = pymysql.connect(host='localhost',
user='root',
password='123456',
db='test',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
# 删除数据
sql = "DELETE FROM `user` WHERE `id`=%s"
cursor.execute(sql, (self.get_argument('id')))
connection.commit()
self.write(tornado.escape.json_encode({'code': 0, 'msg': '删除数据成功'}))
except:
self.write(tornado.escape.json_encode({'code': 1, 'msg': '删除数据失败'}))
finally:
connection.close()
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
(r"/get_data", GetDataHandler),
(r"/update_data", UpdateDataHandler),
(r"/delete_data", DeleteDataHandler),
])
if __name__ == "__main__":
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()
```
注意:以上代码仅供参考,实际应用中需要根据具体情况进行修改。
阅读全文