码迷,mamicode.com
首页 > 其他好文 > 详细

rename_table

时间:2018-01-25 14:15:54      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:time   load   form   pac   else   eid   too   reload   address   

#-*-coding:utf-8 -*- #!/usr/bin/python import sys reload(sys) sys.setdefaultencoding(‘utf-8‘) import os import time import MySQLdb from datetime import datetime def selectTable(conn,defdb,tbname): global plname,tablelist try: cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) #conn.select_db(‘INFORMATION_SCHEMA‘) sql="SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=\‘%s\‘ and TABLE_NAME = \‘%s\‘;" % (defdb,tbname) #print sql cur.execute(sql) result = cur.fetchone() #print result conn.commit() cur.close() except MySQLdb.Error,e: print "Mysql Error %d:%s"%(e.args[0],e.args[1]) return result def rename_table(conn,zoneid,defdb): stri="" global plname,tablelist try: cur = conn.cursor(cursorclass=MySQLdb.cursors.DictCursor) conn.select_db(defdb) sql=" SELECT operateTime from b_flowheromoney order by operateTime limit 1" cur.execute(sql) conn.commit() result=cur.fetchone() if result is not None: print result[‘operateTime‘] oldtime=result[‘operateTime‘] date_space=int(time.time())-oldtime if date_space >60*60*24*10 and date_space >0 and oldtime>60*60: print "新旧表间隔大于10天" for one in tablelist: if selectTable(conn,defdb,"%s_bak" % one) is not None: print "开始 rename table %s to %s_bak" % (one,one) cur.execute("truncate table %s_bak" % (one)) conn.commit() cur.execute("drop table if exists %s_bak") % one conn.commit() cur.execute("rename table %s to %s_bak" % (one)) conn.commit() else: print "rename table 不超过10天,不操作" else: print "新表没有超过10天,不操作备份" cur.close() except MySQLdb.Error,e: print e print "MySQL Error %d:%s" % (e.args[0],e.args[1]) if __name__=="__main__": tablelist=[‘b_flowheromoney‘,‘b_flowmmcache‘,‘b_flowequip‘,‘b_flowheroexp‘,‘b_flowtool‘,‘b_flowherostrength‘,‘b_flowheroattr‘,‘b_flowfabao‘,‘m_logininfo‘,‘m_loginout‘] plname=sys.argv[1] zoneid=sys.argv[2] gsport=sys.argv[3] if os.path.exists("/usr/jyserver/%s/server/config/dbs.properties" % str(gsport)): with open("/usr/jyserver/%s/server/config/dbs.properties" % str(gsport)) as f: for line in f: print line zoneid=int(line.split("=")[0]) print zoneid address=line.split("/")[2].split(":")[0] print address database_name=line.split("&")[0].split("/")[3] print database_name if not zoneid is None and not address is None: defdb="xmxy_%s_%s" % (plname,zoneid) conn = MySQLdb.connect(host="%s" % address,user=‘xxxx‘,passwd=‘xxxx‘,port=3306,charset=‘utf8‘) rename_table(conn,zoneid,defdb) else: print "zoneid and address is null" else: print "table is not exists"

rename_table

标签:time   load   form   pac   else   eid   too   reload   address   

原文地址:http://blog.51cto.com/yzg784534398/2064995

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!