mysql分页查询

 

 

python 3.6

 

复制代码
# -*- coding: utf-8 -*-

import pymysql
import smtplib
import datetime
import time


mysql_server="192.168.1.22"
user_name="root"
password= "yeemiao3040"
db_name="db_admin"

offset = 100



def query_indb(page_number):
    sqltext = "select auto_id,\
           ID,\
           USER,\
           HOST,\
           DB,\
           COMMAND,\
           TIME,\
           STATE,\
           INFO,\
           create_time,\
           update_time\
           from tb_run_long_sql limit %s,%s" %(page_number*offset,offset)
    print(sqltext)
    db = pymysql.connect(mysql_server,user_name, password, db_name,port=3306)
    cursor = db.cursor()
    try:
        cursor.execute(sqltext)
        results = cursor.fetchall()
        record_cnt = len(results)
    except Exception as e:
        print(e)
    db.close()
    return results,record_cnt

def data_to_file(page_number):

    sqltext = "select auto_id,\
           ID,\
           USER,\
           HOST,\
           DB,\
           COMMAND,\
           TIME,\
           STATE,\
           create_time,\
           update_time\
           from tb_run_long_sql_cp limit %s,%s" %(page_number*offset,offset)
    print(sqltext)
    db = pymysql.connect(mysql_server,user_name, password, db_name,port=3306)
    cursor = db.cursor()
    cursor.execute(sqltext)
    results = cursor.fetchall()
    ##print(rows[0][4])
    try:
        with open ("E:/aa.txt",'a+') as fout:
            for row in results:
                fout.write('%s,%s,%s,%s,%s,%s,%s,%s,%s,%s\n'%(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8],row[9]))
    except Exception as e:
        print(e)
    finally:
        db.close()


def gente_total_cnt():
    sql = "select count(1) from tb_run_long_sql_cp"
    db = pymysql.connect(mysql_server,user_name, password, db_name,port=3306)
    cursor = db.cursor()
    try:
        cursor.execute(sql)
        results = cursor.fetchall()
        total_cnt = results[0][0]
    except Exception as e:
        print(e)
    db.close()
    return total_cnt

if __name__ == '__main__':
    print("开始时间:"+time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time())))
    total_cnt = gente_total_cnt()
    print("表记录数:" + str(total_cnt))
    page_cnt = round(total_cnt/offset)
    print("页数:" + str(page_cnt))
    i = 0
    while i <= page_cnt:
        data_to_file(i)
        i = i + 1
复制代码

 

posted @   slnngk  阅读(56)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2019-09-30 mha安装部署
2019-09-30 mha自定义路径安装
2019-09-30 gnuplot输出柱状图
点击右上角即可分享
微信分享提示