# !/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('xx/xx@10.2.10.11/esbdb')
# 创建游标
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 /*+ parallel(t 8)*/ distinct
t.esbserviceflowno,
t.logicchannel,
t.logicsystem,
t.serviceid,
s.description,
t.respcode,
t.respmsg,
p.developer,
p.operator,
p.department,
floor ((esbtimediff(to_timestamp(transtamp4, 'yyyy-mm-dd hh24:mi:ss.xff'),
to_timestamp(transtamp1, 'yyyy-mm-dd hh24:mi:ss.xff'))) / 1024) ctime
from esb2_trans_log t
left join serviceinfo s
on t.serviceid = s.serviceid
left join tlcb_esb_yyzz p
on t.logicsystem=p.name
where t.trans_date >=trunc(sysdate)
and t.trans_date <=trunc(sysdate+1)
and (t.respmsg not like '%Read timed out%' and t.respmsg not like '%异常%' and
t.respmsg not like '%超时%')
and t.respcode = '000000'
and t.flowstepid in ('4','E')
and t.logicsystem in
('NCBS', 'ECIF', 'CNAPS', 'ELINK', 'IBPS', 'ELINKHD', 'GAPS', 'GXP','IBANK')
and floor((esbtimediff(to_timestamp(transtamp4, 'yyyy-mm-dd hh24:mi:ss.xff'),
to_timestamp(transtamp1, 'yyyy-mm-dd hh24:mi:ss.xff'))) / 1024)>1
order by ctime desc
"
mysql = mysql.replace('fffff',aa)
mysql =mysql.replace('ggggg',bb)
print mysql
# 定义一个字典,key为对应的数据类型也用作excel命名,value为查询语句
title='Top交易系统_交易耗时清单'
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)