如何使用python修复特定查询以使用分页
我正在使用flaskpython和mysql设置聊天应用程序的分页,该应用程序只允许页面上的前10个页面如何使用python修复特定查询以使用分页,python,mysql,pagination,Python,Mysql,Pagination,我正在使用flaskpython和mysql设置聊天应用程序的分页,该应用程序只允许页面上的前10个页面 @app.route('/chats', defaults={'page':1}, methods=['GET', 'POST']) @app.route('/chats/page/<int:page>', methods=['GET', 'POST']) def chats(page): if 'lid' in session: id = session['lid'
@app.route('/chats', defaults={'page':1}, methods=['GET', 'POST'])
@app.route('/chats/page/<int:page>', methods=['GET', 'POST'])
def chats(page):
if 'lid' in session:
id = session['lid']
uid = session['uid']
# Create cursor
perpage = 10
startat = page * perpage
cur = mysql.connection.cursor()
# Get message
cur.execute("SELECT * FROM message WHERE (msg_by=%s AND msg_to=%s)
OR (msg_by=%s AND msg_to=% s) limit %s, %s"
"ORDER BY id ASC", (uid, id, id, uid)(startat,perpage))
chats = cur.fetchall()
# Close Connection
cur.close()
return render_template('chats.html', chats=chats,)
return redirect(url_for('login'))
显示错误1064,您的SQL语法有错误;但是我找不到,如果有人能解决这个问题,请帮助,谢谢你,你需要首先使用ORDER BY,然后根据SQL标准应用LIMIT
将sql查询更改为:
cur.executeSELECT*从消息中,其中msg_by=%s和msg_to=%s
或msg_by=%s和msg_to=%s按id排序ASC限制%s,%s,
uid,id,id,UIDStart,每页
@石标此为答案,然后帮助他人