加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python3删除mysql上月分区数据(脚本)

发布时间:2020-12-20 10:49:20 所属栏目:Python 来源:网络整理
导读:import datetime import pymysql import pymysql.cursorstables_schdule =[ " talbe1 " , " table2 " , " table3 " , " table4 " , " table5 " ]x = datetime.date.today().replace(day=1 )y = datetime.timedelta(days=1 )last_month = (x-y).strftime( " %
import datetime
import pymysql
import pymysql.cursors

tables_schdule=["talbe1","table2","table3","table4","table5"]

x = datetime.date.today().replace(day=1)
y = datetime.timedelta(days=1)
last_month = (x-y).strftime("%Y%m")
#print(last_month)

drop_tables=[]
for i in range(5):
    drop_tables.append("ALTER TABLE {} TRUNCATE PARTITION P{};".format(tables_schdule[i],last_month))

#print(drop_tables)

connection=pymysql.connect(127.0.0.1,root,duzhaoqi2015,eos)

try:
#获取一个游标
    with connection.cursor() as cursor:
        # sql=‘select * from test‘
        # cursor.execute(sql)
        for i in drop_tables:
            #print(i)
            cursor.execute(i)
        # for row in cursor.fetchall():
        #     print("ID:" + str(row[0]) + ‘  名字:‘ + row[1])
        # connection.commit()
finally:
    connection.close()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读