__init__.py
# 前提已掛載 swagger 未來寫文章
from sqlalchemy import text
from flask_restx import Namespace,Resource,fields
from .serializer import *
@api.route(‘/example’)
class Example(Resource):
@api.doc(description=“新增範例”)
def post(self):
data = request.get_json()
print(data)
with open(‘example.sql’,’r’,encoding='utf-8') as f:
sql = f.read()
f.close()
with db.engine.begin() as conn:
res = conn.execute(text(sql.format(**data)))
print(res)
conn.commit()
conn.close()
return jsonify({'code':0,'data':res})
example.sql
SELECT [Id]
,[ManageNo]
,[Type]
,[System]
,[Info]
,CASE
WHEN [Status] = 0 THEN '停機中'
WHEN [Status] = 1 THEN '運行中'
WHEN [Status] = 2 THEN '維修中'
END AS [StatusDescription]
FROM [TESTDB].[dbo].[planInfo]
serializer.py
def ExampleSerializer(row):
''' 對應 sp example.sql '''
return dict(zip(("id","manageNo","type","system","info","status"),row))
最近學到很帥又能夠保有以前django在使用序列化器
將查詢到的內容返回key-value,覺得不錯做此紀錄
今年快過完才開始精實起來(笑)
會慢慢將空白一年的事情用文字分享
當一個人生記錄(笑)