1、sql.py
# -- coding: utf-8 --
import web
db = web.database(dbn="mysql",
db="test",
user="root",
host="127.0.0.1",
passwd="123456",
charset="utf8",
port=3306)
db.query("SET NAMES utf8mb4")
def get_all():
return db.query('select * from ztree_test')
def create(body):
try:
ID = body.get('ID')
PID = body.get('PID')
NAME = body.get('NAME')
db.insert("ztree_test", **{"ID": ID, "PID": PID, "NAME": NAME})
except Exception as e:
print e
def update(body):
try:
ID = body.get('ID')
NAME = body.get('NAME')
db.update("ztree_test", where="ID ='%s'" % ID, **{"NAME": NAME})
except Exception as e:
print e
def delete(body):
try:
ID = body.get('ID')
db.delete("ztree_test", where="ID ='%s'" % ID)
except Exception as e:
print e
2、ztest.py
# -- coding: utf-8 --
from flask import Flask, render_template, request
import sql
import json
import uuid
import time
app = Flask(__name__)
@app.route("/ztest")
def ztest():
return render_template("ztest.html")
@app.route("/get")
def get():
infos = []
ztrees = sql.get_all()
if ztrees:
for one in ztrees:
ID = one.ID
PID = one.PID
NAME = one.NAME
tmp = {'ID': ID,
'PID': PID,
'NAME': NAME}
infos.append(tmp)
return json.dumps({"res_dict": infos})
@app.route("/add", methods=['POST'])
def add():
if request.method == "POST":
PID = request.form.get("PID")
NAME = request.form.get("NAME")
ID = createUUID()
tmp = {
"ID": ID,
"PID": PID,
"NAME": NAME
}
sql.create(tmp)
return json.dumps({"ID": tmp.get("ID")})
@app.route("/update", methods=['POST'])
def update():
if request.method == "POST":
ID = request.form.get("ID")
NAME = request.form.get("NAME")
tmp = {
"ID": ID,
"NAME": NAME
}
sql.update(tmp)
return json.dump