使用MySql在Flask中进行分页 [英] Pagination in Flask using MySql

查看:134
本文介绍了使用MySql在Flask中进行分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我进行了很多搜索.我得到的所有文章都包括SQLAlchemy,但都没有涉及mysql.我正在使用flask,我在mysql中有一个数据库,我需要在页面中显示数据.像1000张图像一样,每页10个,所以100页.在mysql中,我们可以借助limit进行分页.路线可以是:

I searched a lot about it. All the articles i get include SQLAlchemy and none of them deal with mysql. I am working with flask and i have a database in mysql and i need to display the data in pages. Like 1000 images, per page 10 so 100 pages. In mysql we can do pagination with the help of limit. And the routes can be:

@app.route('/images', defaults={'page':1})
@app.route('/images/page/<int:page>')

我要问的是,这是分页所需要的吗?还是我在这里忘记了重要的事情?和mysql语法为:

I need to ask is this all that is needed for pagination? or am i forgetting something important here? and mysql syntax would be:

db = mysql.connect('localhost', 'root', 'password', 'img')
cursor = db.cursor()
cursor.execute('SELECT Id,Title,Img FROM image ORDER BY RAND() limit 20 offset 0;')
data = list(cursor.fetchall())

获得前20个结果,但如何根据页面编号获得下一个?Flask-paginate库仅适用于tSQLAlchemy.

to get the first 20 results but how to get the next according to the pageno.? Flask-paginate library works only for tSQLAlchemy.

推荐答案

尝试一下

@app.route('/images', defaults={'page':1})
@app.route('/images/page/<int:page>')
def abc(page):
    perpage=20
    startat=page*perpage
    db = mysql.connect('localhost', 'root', 'password', 'img')
    cursor = db.cursor()
    cursor.execute('SELECT Id,Title,Img FROM image limit %s, %s;', (startat,perpage))
    data = list(cursor.fetchall())

也许这会有所帮助.

这篇关于使用MySql在Flask中进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆