我收到這樣的獲取請求:
http://127.0.0.1:5000/stockList?name=a,b,c,
我想要的只是從資料庫中過濾那些昏迷值。像這樣:
name = request.args.get('name', None)
connection = db.engine.connect(close_with_result=True)
sql = text("""select * from stockstatus where name='a' or name='b' or name='c'""")
connection.execute(sql)
connection.close()
這是表的樣子:
-- id | name |
-- ---- ---------
-- 1 | a |
-- 2 | b |
-- 3 | c |
-- 4 | d |
-- 5 | e |
我只想要 get 引數中提到的 a,b,c 值如何通過引數燒瓶中的昏迷值進行過濾?
uj5u.com熱心網友回復:
您應該將引數傳遞給text方法:
name = request.args.get('name')
if name:
names = name.split(',')
else:
names = []
connection = db.engine.connect(close_with_result=True)
sql = text("""select * from stockstatus where name IN :names""", names=tuple(names))
connection.execute(sql)
connection.close()
本主題可能會有所幫助:SQLAlchemy IN 子句
uj5u.com熱心網友回復:
我建議你做這樣的事情:
@app.route('/stockList/<a>/<b>/<c>')
def stockList(a=None, b=None, c=None):
這種方法應該更安全。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/382703.html
標籤:Python 烧瓶 sqlalchemy
