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])

 

posted @ 2017-03-20 17:10  cphmvp  阅读(352)  评论(0编辑  收藏  举报
爬虫在线测试小工具: http://tool.haoshuju.cn/