python mysql 分页程序
来源:互联网 发布:如何用java做表格 编辑:程序博客网 时间:2024/06/09 17:02
#!/usr/bin/env python# -*- coding: utf-8 -*-
#author:djangofan@163.com
import MySQLdbimport os import hashlib#DB parameterstrHost = 'localhost'strDB = 'bigo_db'strUser = 'root'strPasswd = ''def seEncode(ustr, encoding='utf-8'): '''负责把入数据库的字符串,转化成utf-8编码''' if ustr is None: return '' if isinstance(ustr, unicode): return ustr.encode(encoding, 'ignore') else: return str(ustr)#connect to DBdef getConnect(db=strDB, host=strHost, user=strUser, passwd=strPasswd, charset="utf8"): return MySQLdb.connect(host=strHost, db=strDB, user=strUser, passwd=strPasswd, charset="utf8")def initClientEncode(conn): '''mysql client encoding=utf8''' curs = conn.cursor() curs.execute("SET NAMES utf8") conn.commit() return cursclass MySQLQueryPagination(object): def __init__(self,conn,numPerPage = 20): self.conn = conn self.numPerPage = numPerPage def queryForList(self,sql,param = None): totalPageNum = self.__calTotalPages(sql,param) for pageIndex in range(totalPageNum): yield self.__queryEachPage(sql,pageIndex,param) def __createPaginaionQuerySql(self,sql,currentPageIndex): startIndex = self.__calStartIndex(currentPageIndex) qSql = r'select * from (%s) total_table limit %s,%s' % (sql,startIndex,self.numPerPage) return qSql def __queryEachPage(self,sql,currentPageIndex,param = None): curs = initClientEncode(self.conn) qSql = self.__createPaginaionQuerySql(sql, currentPageIndex) if param is None: curs.execute(qSql) else: curs.execute(qSql,param) result = curs.fetchall() curs.close() return result def __calStartIndex(self,currentPageIndex): startIndex = currentPageIndex * self.numPerPage; return startIndex; def __calTotalRowsNum(self,sql,param = None): ''' 计算总行数 ''' tSql = r'select count(*) from (%s) total_table' % sql curs = initClientEncode(self.conn) if param is None: curs.execute(tSql) else: curs.execute(tSql,param) result = curs.fetchone() curs.close() totalRowsNum = 0 if result != None: totalRowsNum = int(result[0]) return totalRowsNum def __calTotalPages(self,sql,param): ''' 计算总页数 ''' totalRowsNum = self.__calTotalRowsNum(sql,param) totalPages = 0; if (totalRowsNum % self.numPerPage) == 0: totalPages = totalRowsNum / self.numPerPage; else: totalPages = (totalRowsNum / self.numPerPage) + 1 return totalPages def __calLastIndex(self, totalRows, totalPages,currentPageIndex): '''计算结束时候的索引''' lastIndex = 0; if totalRows < self.numPerPage: lastIndex = totalRows; elif ((totalRows % self.numPerPage == 0) or (totalRows % self.numPerPage != 0 and currentPageIndex < totalPages)) : lastIndex = currentPageIndex * self.numPerPage elif (totalRows % self.numPerPage != 0 and currentPageIndex == totalPages): # 最后一页 lastIndex = totalRows return lastIndex
if __name__ == '__main__': conn = getConnect() pag = MySQLQueryPagination(conn) sql = r'SELECT * FROM `websit_base_info` WHERE id>%s' param = [3] for ret in pag.queryForList(sql,param): print ret conn.close()
这个对付100万以下的,应该没什么问题,当大数据量时,需要改进; 大部分情况下是可以满足要求的,具体的见程序吧,已经写的很清新了
- python mysql 分页程序
- jsp + mysql 的分页程序
- python mysql盲注小程序
- Python Flask Mysql链式查询/分页
- 基于mysql的分页程序完全解决方案(含普通分页/分段分页/原始分页/微博的since_id类分页)
- 一个简单的mysql数据库分页的程序模板
- 用java程序实现mysql的分页效果
- 分页程序
- 分页程序
- 分页程序
- 分页程序
- 分页程序
- 分页程序
- 分页程序
- 分页程序
- MYSQL分页
- MySql分页
- mysql 分页
- 双顶
- NS应用实例4
- mysql 顺序号 示例
- ubuntu下单机安装Cassandra-0.7.8
- iOS中对文件的操作
- python mysql 分页程序
- tomcat 和 jboss的热部署(热发布)问题 .
- DrawDib函数组的使用
- ArcGIS中的几何对象——点
- 一致性哈希算法及其在分布式系统中的应用
- Java的synchronized关键字:同步机制总结
- 短线盈亏指标
- TEDxGuangzhou report
- Windows下控制台输出