python mysql 快速分割导出到CSV

import random,string,re,time,sys,hashlib,pymysql,requests
connect = pymysql.connect(user = 'm',
                password = 'h',
                db = 'x',
                host = 'r',
                port = 3306,
                charset = 'utf8'
                )
con = connect.cursor()
def get_all(sql, params=()):
    global con,connect
    result = None
    try:
        con.execute(sql, params)
        result = con.fetchall()
        #.close()
    except Exception as e:
        print(e)
    return result
def get_one(sql, params=()):
    global con,connect
    result = None
    try:
        con.execute(sql, params)
        result = con.fetchone()
        #.close()
    except Exception as e:
        print(e)
    return result
sql1="SHOW TABLES"
aa=get_all(sql1)
for ii in aa:
    ax=ii[0]
    #print(ax)
    sql2="select count(*) from "+ax
    ab=get_all(sql2)
    av=ab[0]
    print(av)
    xxm=1
    for im in range(0,av[0],500000):
        if xxm==1:
            xxs="select * from "+ax+" limit "+str(im)+","+str(im+500000)+" into outfile 'd:/tu/"+ax+str(xxm)+".csv'"
        else:
            xxs="select * from "+ax+" limit "+str(im+1)+","+str(im+500000)+" into outfile 'd:/tu/"+ax+str(xxm)+".csv'"
        print(xxs)
        xxm=xxm+1

 

posted @ 2023-07-28 07:57  myrj  阅读(109)  评论(0编辑  收藏  举报