zoukankan      html  css  js  c++  java
  • python_简单的DB统计

    import numpy as np
    import pylab as pl
    dates=['20170314','20170315','20170316','20170317','20170318','20170319']
    x = range(len(dates))
    y = [1974786,3502609,3824524,2046503,5623141]
    y.append(5505794)
    pl.plot(x,y,'ro-')
    pl.xticks(x, dates, rotation=45)
    pl.margins(0.08)
    pl.subplots_adjust(bottom=0.15)
    pl.grid(True)
    pl.show()

    含DB的统计 包含图展示

    import MySQLdb
    import numpy as np
    import pylab as pl
    import datetime
    def analysisData(conn='test',tablename='test'):
        today=datetime.date.today()
        date1=str(today-datetime.timedelta(days=6))
        date2=str(today-datetime.timedelta(days=5))
        date3=str(today-datetime.timedelta(days=4))
        date4=str(today-datetime.timedelta(days=3))
        date5=str(today-datetime.timedelta(days=2))
        date6=str(today-datetime.timedelta(days=1))
        date7=str(today)
        date8=str(today+datetime.timedelta(days=1))
        dates=[date1,date2,date3,date4,date5,date6,date7]
        x = range(len(dates))
        y = []
        sql='select "'+date1+'",count(*) from '+tablename+' where  time> unix_timestamp("'+date1+'") and time <  unix_timestamp("'+date2+'")'
        cur.execute(sql)
        result=cur.fetchone()
        print result
        y.append(result[1])
        sql='select "'+date2+'",count(*) from '+tablename+' where  time> unix_timestamp("'+date2+'") and time <  unix_timestamp("'+date3+'")'
        cur.execute(sql)
        result=cur.fetchone()
        print result
        y.append(result[1])
        sql='select "'+date3+'",count(*) from '+tablename+' where  time> unix_timestamp("'+date3+'") and time <  unix_timestamp("'+date4+'")'
        cur.execute(sql)
        result=cur.fetchone()
        print result
        y.append(result[1])
        sql='select "'+date4+'",count(*) from '+tablename+' where  time> unix_timestamp("'+date4+'") and time <  unix_timestamp("'+date5+'")'
        cur.execute(sql)
        result=cur.fetchone()
        print result
        y.append(result[1])
        sql='select "'+date5+'",count(*) from '+tablename+' where  time> unix_timestamp("'+date5+'") and time <  unix_timestamp("'+date6+'")'
        cur.execute(sql)
        result=cur.fetchone()
        print result
        y.append(result[1])
        sql='select "'+date6+'",count(*) from '+tablename+' where  time> unix_timestamp("'+date6+'") and time <  unix_timestamp("'+date7+'")'
        cur.execute(sql)
        result=cur.fetchone()
        print result
        y.append(result[1])
        sql='select "'+date7+'",count(*) from '+tablename+' where  time> unix_timestamp("'+date7+'") and time <  unix_timestamp("'+date8+'")'
        cur.execute(sql)
        result=cur.fetchone()
        print result
        y.append(result[1])
        pl.plot(x, y)
        pl.xticks(x, dates, rotation=45)
        pl.grid(True)
        pl.show()
    try:
        today=datetime.date.today()
        date1=str(today-datetime.timedelta(days=6))
        date2=str(today-datetime.timedelta(days=5))
        date3=str(today-datetime.timedelta(days=4))
        date4=str(today-datetime.timedelta(days=3))
        date5=str(today-datetime.timedelta(days=2))
        date6=str(today-datetime.timedelta(days=1))
        date7=str(today)
        date8=str(today+datetime.timedelta(days=1))
        dates=[date1,date2,date3,date4,date5,date6,date7]
        x = range(len(dates))
        y = []
        conn=MySQLdb.connect(host='192.168.*.*',user='ulog',passwd='***',db='log_platform',port=8888)
        cur=conn.cursor()
        tablename='tblDayiPcLog0'
        print tablename
        analysisData(conn,tablename)
        tablename='tblDayiPcLog1'
        print tablename
        analysisData(conn,tablename)
        tablename='tblDayiPcLog2'
        print tablename
        analysisData(conn,tablename)
        tablename='tblDayiPcLog3'
        print tablename
        analysisData(conn,tablename)
        tablename='tblDayiPcLog4'
        print tablename
        analysisData(conn,tablename)
        tablename='tblDayiPcLog5'
        print tablename
        analysisData(conn,tablename)
        tablename='tblDayiPcLog6'
        print tablename
        analysisData(conn,tablename)
        tablename='tblDayiPcLog7'
        print tablename
        analysisData(conn,tablename)
        tablename='tblDayiPcLog8'
        print tablename
        analysisData(conn,tablename)
        tablename='tblDayiPcLog9'
        print tablename
        analysisData(conn,tablename)
        cur.close()
        conn.close()
    except MySQLdb.Error,e:
        print "err"%(e.args[0],e.args[1])
  • 相关阅读:
    Spark 分布式环境---slave节点无法启动(已解决)
    Hadoop 分布式环境slave节点重启忽然不好使了
    Spark 2.2.0 分布式集群环境搭建
    arm-linux-gcc
    VS+QT创建一个OpenCV应用
    QT安装和vs2015使用
    DE1_MSEL
    Hog实例
    resize函数
    Hog特征
  • 原文地址:https://www.cnblogs.com/cphmvp/p/6590335.html
Copyright © 2011-2022 走看看