Python by Month table

Source: Internet
Author: User

#!/usr/bin/env python

-coding:utf-8-

Import time
From datetime import datetime
Import Calendar
Import MySQLdb
Import Sys

Class Getdaymonth (object):

  def getlastdayoflastmonth (self,step): D = DateTime.Now () year = d.year month = D.month if month = = 1:month = YEAR-= 1 Else:month-= Step days = Calendar.monthrange (year, month) [1] retur n DateTime (year, month, days). Strftime ('%y-%m-%d ') def getfirstdayoflastmonth (self,step): D = DateTime.Now () year = d . Year month = D.month if month = = 1:month = year-= 1 Else:month-= step return Date Time (year, month, 1). strftime ('%y-%m-%d ') def getlastmonth (self,step): D = DateTime.Now () year = d.year month = d. Month if month = = 1:month = year-= 1 Else:month-= Step return datetime (year, month, 1 ). Strftime ('%y%m ')  

Class Mysqlexe (object):
def Init(self,host,port,user,passwd,db,charset= ' UTF8 '):
Try
Self.conn = MySQLdb.connect (Host,user,passwd,db,int (port))
Except Mysqldb.error as E:
ErrorMsg = ' cannot connect to Server\nerror (%s):%s '% (E.args[0],e.args[1])
Print ErrorMsg
Exit (2)
Self.cursor = Self.conn.cursor ()

def Creat_table(self,sql):    try:        self.cursor.execute(sql)        self.conn.commit()    except:        self.conn.rollback()        print "sql execute error"        exit(2)def __del__(self):    self.conn.close()    self.cursor.close()

If name = = 'main':

d = GetDayMonth()table_name = sys.argv[1]first_day_month = d.getFirstDayOfLastMonth(1) last_day_month = d.getLastDayOfLastMonth(1)last_3day_month = d.getLastDayOfLastMonth(3)last_month = d.getLastMonth(1)mysql_exec = MysqlExe(‘127.0.0.1‘,3306,‘root‘,‘XXXX‘,‘guanba_data‘)sql_create = "create table guanba_data.%s_%s  like guanba_data.%s"%(table_name,last_month,table_name)sql_insert = "insert into guanba_data.%s_%s select * from guanba_data.%s where create_time >= unix_timestamp(‘%s 00:00:00‘) and create_time <= unix_timestamp(‘%s 23:59:59‘)"%(table_name,last_month,table_name,first_day_month,last_day_month)sql_del = "delete from guanba_data.%s where  create_time <= unix_timestamp(‘%s 23:59:59‘)"%(table_name,last_3day_month)mysql_exec.Creat_table(sql_create)mysql_exec.Creat_table(sql_insert)mysql_exec.Creat_table(sql_del)

Python by Month table

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.