小编给大家分享一下Python如何批量删除只保留最近几天table,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
Python批量删除table,只保留最近几天的table
代码如下:
#!/usr/bin/python3 """ 批量删除table,只保留最近几天的table """ import pymysql import re def conn_(host='',usr='',passwd='',db='',port=3306,): conn = pymysql.connect(host, usr, passwd, db, port,charset='utf8') return conn def del_table(conn_,table_pre='',table_suff='%Y%m%d',keep_count=3): date_form = None if table_suff == "%Y%m%d": date_form = "_(\d{4}\d{1,2}\d{1,2})$" date_len = 8 elif table_suff == "%Y-%m-%d": date_form = "_(\d{4}-\d{1,2}-\d{1,2})$" date_len = 10 elif table_suff == "%Y%m": date_form = "_(\d{4}\d{1,2})$" date_len = 6 elif table_suff == "%Y-%m": date_form = "_(\d{4}-\d{1,2})$" date_len = 7 else: raise Exception("暂时不支持其他类型的时间后缀") curs = conn_.cursor() curs.execute('SHOW TABLES') data = curs.fetchall() table_ = r'%s'%table_pre+date_form list_table = [] i = 0 for table in data: mt = re.search(table_, table[0]) if mt: if len(mt.groups()[0]) == date_len: list_table.append((table[0], mt.groups()[0])) i += 1 sorted(list_table, key=lambda date: date[1]) #按照表结构后缀时间升序排序 for j in range(i-keep_count): sql = 'DROP TABLE if exists %s'%list_table[j][0] curs.execute(sql) curs.close() conn_.close() if __name__ == '__main__': table_pre = "tree_product" table_suff = "%Y%m%d" # table_suff = "%Y-%m-%d" # table_suff = "%Y%m" # table_suff = "%Y-%m" conn=conn_('10.0.0.11','root','sctele@root','sxf',port=3306) del_table(conn,table_pre=table_pre,table_suff=table_suff,keep_count=1)
看完了这篇文章,相信你对“Python如何批量删除只保留最近几天table”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。