python之mysql分页

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/8/1 16:47
# @Author  : Jack Wu
# @File    : 分页.py
import pymysql


def query_to_test(last_id, is_next):
    conn = pymysql.connect(host='172.16.2.62', port=3306, user='root', passwd='666', db='test', charset='utf8')
    cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
    if is_next: # 上一页
        cursor.execute('select * from test where nid>%s limit 10', last_id)
        ret = cursor.fetchall()
    else:   # 下一页
        cursor.execute('select * from test where nid<%s order by nid desc limit 10', last_id)
        ret = cursor.fetchall()
        ret = list(reversed(ret))

    conn.commit()
    cursor.close()
    conn.close()
    return ret


current_first_id = 0
current_last_id = 0
while True:
    p = input('1.上一页 | 2.下一页 >>>')
    if p == '2':
        is_next = True
        ret = query_to_test(current_last_id, is_next)
    else:
        is_next = False
        ret = query_to_test(current_first_id, is_next)
        # current_last_id = current_last_id + 1
    
    current_first_id = ret[0]['nid']    # 结果集开始id
    current_last_id = ret[-1]['nid']    # 结果集结束id
    print(ret)
    # for i in ret:
    #     print(i)

 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值