python之mysql分页

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yan7895566/article/details/81335349
#!/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)

猜你喜欢

转载自blog.csdn.net/yan7895566/article/details/81335349
今日推荐