python 插入数据

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import datetime
import smtplib
import string
import  MySQLdb
Yesterday=datetime.datetime.now() - datetime.timedelta(days=1)
yesterday=Yesterday.strftime('%Y-%m-%d')
logfile="/app/newgetui/wyethService/current/logs/DataApiInfo_%s-0.log" %yesterday
sumline=0
type1=0
type2=0
type3=0
type4=0
type5=0
type6=0
type7=0
type8=0
with file(logfile,'r') as lock_f:
        for line in lock_f:
                if line.find("Result info") > -1:
                        sumline=sumline+1
                else: 
                        print "no file"
                if  line.find("checkResult=1") > -1:
                        type1=type1+1
                elif  line.find("checkResult=2") > -1:
                        type2=type2+1
                elif  line.find("checkResult=3") > -1:
                        type3=type3+1
                elif  line.find("checkResult=4") > -1:
                        type4=type4+1
                elif  line.find("checkResult=5") > -1:
                        type5=type5+1
                elif  line.find("checkResult=6") > -1:
                        type6=type6+1
                elif  line.find("checkResult=7") > -1:
                         type7=type7+1
                elif  line.find("checkResult=8") > -1:
                        type8=type8+1
print type8

def intomysql():
    try:   
        conn=MySQLdb.connect(host='xxxxx',user='xxxx',passwd='xxxx',port=3306,db='xxx')
        cur=conn.cursor()
        yesterdaystr='"%s"' %yesterday
        cur.execute('insert into wyeth_offline_interface_count values(null,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)' %(yesterdaystr,type1,type2,type3,type4,type5,type6,type7,type8,sumline))
        conn.commit()
        cur.close()
        conn.close()    

    except MySQLdb.Error,e:
        print 'insert faild:',e
intomysql()

HOST = "xxxx.20.102"    #定义smtp主机

SUBJECT = "wyeth线下接口查询统计"    #定义邮件主题

TO = ["zhoull@getui.com"]   #定义邮件收件人


FROM = "xxxxxx"    #定义邮件发件人

text = " %s 惠氏线下接口主机122查询统计如下 \n checkResult1 num %s \n checkResult2 num %s \n checkResult3 num %s \n checkResult4 num %s \n checkResult5 num %s \n checkResult6 num %s \n checkResult7 num %s \n checkResult8 num %s \n 合计 num %s \n"  %(yesterday,type1,type2,type3,type4,type5,type6,type7,type8,sumline)    #邮件内容

BODY = string.join((    #组装sendmail方法的邮件主体内容,各段以"\r\n"进行分隔

        "From: %s" % FROM,

        "To: %s" % TO,

        "Subject: %s" % SUBJECT ,

        "",

        text

        ), "\r\n")

server = smtplib.SMTP()    #创建一个SMTP()对象

server.connect(HOST,"25")    #通过connect方法连接smtp主机

server.starttls()    #启动安全传输模式

server.login("xxxxx","xxxxx")    #邮箱账号登录校验

for i in range(0,1):
        server.sendmail(FROM, TO[i], BODY)    #邮件发送

server.quit()    


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值