PYTHON mysql形成分割文件

import random,string,re,time,sys,hashlib,pymysql,requests
f=open("aa.txt","w")
connect = pymysql.connect(user = 'root',
                password = '123456',
                db = 'new',
                host = '127.0.0.1',
                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
ss="show columns from "+"td_gov_company_abnormal"
ab=get_all(ss)
ak=""
abc=""
xx=0
for ii in ab:
    ak=ak+" "+ii[0]
    xx=xx+1
    abc=abc+"v"+str(xx)+" "
print(ak)
print("rename"+"("+abc+")"+"("+ak+")")

sys.exit(0)
sql1="SHOW TABLES"
aa=get_all(sql1)
abx=0
##aa=["td_gov_company_abnormal","td_gov_company_announcementcourt","td_gov_company_branch","td_gov_company_certificate","td_gov_company_changerecord","td_gov_company_clients","td_gov_company_commontaxpayer","td_gov_company_directors","td_gov_company_discredit","td_gov_company_email","td_gov_company_extend"
##    ,"td_gov_company_firmproduct","td_gov_company_icpinfo","td_gov_company_importandexport","td_gov_company_invest","td_gov_company_jingpin","td_gov_company_license"]
for ii in aa:
    #print(ax)
    if ii[0].startswith("id") or ii[0].startswith("my"):
        continue
    sql2="select count(*) from "+ii[0]
    ab=get_all(sql2)
    print(ab)
    xxm=1
    for iv in range(0,ab[0][0],50000):
        if xxm==1:
            xxs="select * from "+ii[0]+" limit "+str(iv)+","+str(iv+50000)+" into outfile 'd:/tu/"+ii[0]+str(xxm)+".csv';"
        else:
            if iv+50000>=ab[0][0]:
                xxs="select * from "+ii[0]+" limit "+str(iv+1)+","+str(ab[0][0])+" into outfile 'd:/tu/"+ii[0]+str(xxm)+".csv';"
            else:
                xxs="select * from "+ii[0]+" limit "+str(iv+1)+","+str(iv+50000)+" into outfile 'd:/tu/"+ii[0]+str(xxm)+".csv';"
        xxm=xxm+1
        print(xxs)
        f.write(xxs+"\n")
f.close

 

posted @ 2023-07-29 19:12  myrj  阅读(14)  评论(0编辑  收藏  举报