我正在设置聊天应用程序的分页,该应用程序只能使用flask python和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']
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语法有错误;但是我找不到,如果有人在那里解决此问题,请帮助
答案 0 :(得分:0)
您需要先使用ORDER BY
,然后根据SQL标准应用LIMIT
。
将您的sql查询更改为:
cur.execute("SELECT * FROM message WHERE (msg_by=%s AND msg_to=%s)
OR (msg_by=%s AND msg_to=% s) ORDER BY id ASC limit %s, %s",
(uid, id, id, uid)(startat,perpage))