Python 烧瓶,打破查询? @app.route(“/getEmployees/”) def all(用户ID): 对于范围(0200000,50)内的i: g、 db=连接_db() cur=g.db.execute('select*from employees limit 50 offset'+str(i)) 对于cur.fetchall()中的行,条目=[dict(emp\u no=行[0],出生日期=行[1],名字=行[2],姓氏=行[3],性别=行[4],雇用日期=行[5]) g、 db.close() 返回str(条目)
所以我想把所有员工分为50人一组返回。该数据库有200000个条目。以这种方式使用return语句将只返回前50个条目并退出函数。如何以另一种方式实现此逻辑?您可以通过跟踪上次请求期间使用的最后一个偏移量来完成此操作Python 烧瓶,打破查询? @app.route(“/getEmployees/”) def all(用户ID): 对于范围(0200000,50)内的i: g、 db=连接_db() cur=g.db.execute('select*from employees limit 50 offset'+str(i)) 对于cur.fetchall()中的行,条目=[dict(emp\u no=行[0],出生日期=行[1],名字=行[2],姓氏=行[3],性别=行[4],雇用日期=行[5]) g、 db.close() 返回str(条目),python,database,flask,sqlite,Python,Database,Flask,Sqlite,所以我想把所有员工分为50人一组返回。该数据库有200000个条目。以这种方式使用return语句将只返回前50个条目并退出函数。如何以另一种方式实现此逻辑?您可以通过跟踪上次请求期间使用的最后一个偏移量来完成此操作 @app.route('/getEmployees/<userid>') def all(userid): for i in range (0,200000,50): g.db = connect_db() cur
@app.route('/getEmployees/<userid>')
def all(userid):
for i in range (0,200000,50):
g.db = connect_db()
cur=g.db.execute('select * from employees limit 50 offset '+ str(i))
entry=[dict(emp_no=row[0],birth_date=row[1],first_name=row[2],last_name=row[3],gender=row[4],hire_date=row[5]) for row in cur.fetchall()]
g.db.close()
return str(entry)
你想在第一次通话中使用所有数据块,还是想在连续通话中使用下一个50个数据块?嘿,伙计,每次我运行请求时,我只会获得前50个数据块。get(')在我的web浏览器上运行良好,有什么想法吗?@rohit抱歉,最后更新了示例。必须切换到
requests.Session()
,以确保在调用之间保留会话数据。
from flask import session, request
app = Flask(__name__)
@app.route('/getEmployees/<userid>')
def all(userid):
# Allow the user to specify an offset via querystring
offset = request.args.get('offset')
# Allow user to specify limit but default to 50
limit = request.args.get('limit', 50)
if offset is None:
# Look in the session and default to 0 if not defined
offset = session.get('offset', 0)
# Do your query
g.db = connect_db()
cur = g.db.execute('select * from employees limit %d offset %d' % (limit, int(offset)))
entry = [dict(emp_no=row[0],birth_date=row[1],first_name=row[2],last_name=row[3],gender=row[4],hire_date=row[5]) for row in cur.fetchall()]
g.db.close()
# Store the current offset in the session so it is available next request
session['offset'] = offset + limit;
return str(entry)
import requests
s = requests.Session()
s.get('http://localhost/getEmployees/1') # Get first 50
s.get('http://localhost/getEmployees/1') # Get next 50
params = {'offset': 0};
s.get('http://localhost/getEmployees/1', params=params) # Get First 50