# !/usr/bin/env python
# -*- coding: utf-8 -*-
import MySQLdb
from datetime import datetime
import cx_Oracle
import os
import xlwt
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
import datetime #导入日期时间模块
today = datetime.date.today() #获得今天的日期
yesterday = today - datetime.timedelta(days=1)
def get_data(sql):
# 创建数据库连接.
conn = cx_Oracle.connect('')
# 创建游标
cur = conn.cursor()
# 执行查询,
cur.execute(sql)
# 由于查询语句仅会返回受影响的记录条数并不会返回数据库中实际的值,所以此处需要fetchall()来获取所有内容。
result = cur.fetchall()
# 关闭游标
cur.close()
# 关闭数据库连接
conn.close
# 返给结果给函数调用者。
print result
return result
def write_data_to_excel(name, sql):
# 将sql作为参数传递调用get_data并将结果赋值给result,(result为一个嵌套元组)
result = get_data(sql)
# 实例化一个Workbook()对象(即excel文件)
wbk = xlwt.Workbook(encoding='utf-8')
# 新建一个名为Sheet1的excel sheet。此处的cell_overwrite_ok =True是为了能对同一个单元格重复操作。
sheet = wbk.add_sheet('Sheet1', cell_overwrite_ok=True)
# 获取当前日期,得到一个datetime对象如:(2016, 8, 9, 23, 12, 23, 424000)
#today = datetime.today()
# 将获取到的datetime对象仅取日期如:2016-8-9
#today_date = datetime.date(today)
# 遍历result中的没个元素。
titlelist=['交易日期','全局流水号','服务场景码','服务场景码描述','消费方','服务方','返回码','返回信息']
# for i in xrange(len(titlelist)):
# sheet.write(1, i, titlelist[i])
# wbk.save(name + str(yesterday) + '.xls')
for i in xrange(len(result)):
# 对result的每个子元素作遍历,
if i==0:
for x in xrange(len(titlelist)):
sheet.write(0, x, titlelist[x])
for j in xrange(len(result[i])):
y=i+1
# 将每一行的每个元素按行号i,列号j,写入到excel中。
sheet.write(y, j, result[i][j])
# 以传递的name+当前日期作为excel名称保存。
wbk.save(name + str(yesterday) + '.xls')
# 如果该文件不是被import,则执行下面代码。
if __name__ == '__main__':
aa= 'to_date' + "('" + str(yesterday) + ' ' + "00:00:00'," + " 'yyyy-mm-dd hh24:mi:ss')"
bb= 'to_date' + "('" + str(yesterday) + ' ' + "23:59:59'," + " 'yyyy-mm-dd hh24:mi:ss')"
mysql="select to_char(t.trans_date,'yyyy-mm-dd Hh24:mm:ss') as 交易日期,t.esbserviceflowno as 全局流水号,t.serviceid as 服务场景码, s.description as 服务场景码描述,t.logicchannel as 消费方,t.logicsystem as 服务方,t.respcode as 返回码,t.respmsg as 返回信息
from esb2_trans_log t,serviceinfo s
where t.trans_date >= fffff
and t.trans_date <= ggggg
/*and t.logicsystem = 'LOAN'*/
and t.serviceid=s.serviceid
and (t.respmsg like '%Read timed out%' or t.respmsg like '%异常%' or
t.respmsg like '%超时%')
and t.esbserviceflowno is not null
and t.respcode<>'000000'
and (t.logicsystem<>'AAAA' or t.respmsg <>'TGT已超时')
AND (t.logicsystem<>'ELINK' or t.respcode<>'3040')
AND (t.logicsystem<>'IBPS' or t.respcode<>'600011')
AND (t.logicsystem<>'CNAPS' or t.respcode<>'AHVB001')
AND (t.logicsystem<>'CNAPS' or t.respcode<>'UPC002')
AND (t.logicsystem<>'CNAPS' or t.respcode<>'UPC002')
AND (t.logicsystem<>'ELINK' or t.respcode<>'Y999')
and (t.logicsystem<>'ELINK' or t.respcode<>'600011')
and (t.logicsystem<>'CIPS' or t.respcode<>'500101092209')
and (t.logicsystem<>'CCFS' or t.respcode<>'500101092209')
and (t.logicsystem<>'ELINK' or t.respcode<>'0201')
order by 1,5"
mysql = mysql.replace('fffff',aa)
mysql =mysql.replace('ggggg',bb)
print mysql
# 定义一个字典,key为对应的数据类型也用作excel命名,value为查询语句
title='ESB_日报_'
title=title.decode('utf-8').encode('gbk')
db_dict = {title: mysql}
# 遍历字典每个元素的key和value。
for k, v in db_dict.items():
# 用字典的每个key和value调用write_data_to_excel函数。
write_data_to_excel(k, v)