圆月山庄资源网 Design By www.vgjia.com
本文实例讲述了Python实现将MySQL数据库表中的数据导出生成csv格式文件的方法。分享给大家供大家参考,具体如下:
#!/usr/bin/env python # -*- coding:utf-8 -*- """ Purpose: 生成日汇总对账文件 Created: 2015/4/27 Modified:2015/5/1 @author: guoyJoe """ #导入模块 import MySQLdb import time import datetime import os #日期 today = datetime.date.today() yestoday = today - datetime.timedelta(days=1) #对账日期 checkAcc_date = yestoday.strftime('%Y%m%d') #对账文件目录 fileDir = "/u02/filesvrd/report" #SQL语句 sqlStr1 = 'SELECT distinct pay_custid FROM dbpay.tb_pay_bill WHERE date_acct = %s' #总笔数|成功交易笔数|成功交易金额|退货笔数|退货金额|撤销笔数|撤销金额 sqlStr2="""SELECT totalNum,succeedNum,succeedAmt,returnNum,returnAmt,revokeNum,revokeAmt FROM (SELECT count(order_id) AS totalNum FROM (SELECT p.order_id as order_id FROM dbpay.tb_pay_bill p, dbpay.tb_paybillserial q WHERE p.oid_billno = q.oid_billno AND p.paycust_accttype = 2 AND p.Paycust_Type = 1 AND p.stat_bill in (0, 4) AND q.pay_stat = 1 AND q.col_stat = 1 AND p.pay_custid = %s AND q.date_acct = %s UNION ALL SELECT p.order_id as order_id FROM dbpay.tb_pay_bill p, dbpay.tb_paybillserial q WHERE p.oid_billno = q.oid_billno AND p.col_accttype = 2 AND p.col_type = 1 AND p.stat_bill in (0, 4) AND q.pay_stat = 1 AND q.col_stat = 1 AND p.col_custid = %s AND q.date_acct = %s UNION ALL SELECT R.ORDER_ID AS ORDER_ID FROM DBPAY.TB_REFUND_BILL R, DBPAY.TB_PAYBILLSERIAL Q WHERE R.oid_refundno = Q.OID_BILLNO AND R.ORI_COL_ACCTTYPE = 2 AND R.ORI_COL_TYPE = 1 AND R.STAT_BILL = 2 AND Q.PAY_STAT = 1 AND Q.COL_STAT = 1 AND R.ORI_COL_CUSTID = %s AND Q.DATE_ACCT = %s ) as total) A, (SELECT count(order_id) succeedNum ,sum(amt_paybill) succeedAmt FROM (SELECT p.order_id as order_id, q.amt_payserial/1000 as amt_paybill FROM dbpay.tb_pay_bill p, dbpay.tb_paybillserial q WHERE p.oid_billno = q.oid_billno AND p.paycust_accttype = 2 AND p.Paycust_Type = 1 AND p.stat_bill = '0' AND q.pay_stat = 1 AND q.col_stat = 1 AND p.pay_custid = %s AND q.date_acct = %s UNION ALL SELECT p.order_id as order_id, q.amt_payserial/1000 as amt_paybill FROM dbpay.tb_pay_bill p, dbpay.tb_paybillserial q WHERE p.oid_billno = q.oid_billno AND p.col_accttype = 2 AND p.col_type = 1 AND p.stat_bill = '0' AND q.pay_stat = 1 AND q.col_stat = 1 AND p.col_custid = %s AND q.date_acct = %s ) as succeed) B, (SELECT count(order_id) returnNum, sum(amt_paybill) returnAmt FROM (SELECT R.ORDER_ID AS ORDER_ID, Q.AMT_PAYSERIAL/1000 AS AMT_PAYBILL FROM DBPAY.TB_REFUND_BILL R, DBPAY.TB_PAYBILLSERIAL Q WHERE R.oid_refundno = Q.OID_BILLNO AND R.ORI_COL_ACCTTYPE = 2 AND R.ORI_COL_TYPE = 1 AND R.STAT_BILL = 2 AND Q.PAY_STAT = 1 AND Q.COL_STAT = 1 AND R.ORI_COL_CUSTID = %s AND Q.DATE_ACCT = %s ) as retur) C, (SELECT count(order_id) revokeNum,sum(amt_paybill) revokeAmt FROM (SELECT p.order_id as order_id, q.amt_payserial/1000 as amt_paybill FROM dbpay.tb_pay_bill p, dbpay.tb_paybillserial q WHERE p.oid_billno = q.oid_billno AND p.paycust_accttype = 2 AND p.Paycust_Type = 1 AND p.stat_bill = '4' AND q.pay_stat = 1 AND q.col_stat = 1 AND p.pay_custid = %s AND q.date_acct = %s UNION ALL SELECT p.order_id as order_id, q.amt_payserial/1000 as amt_paybill FROM dbpay.tb_pay_bill p, dbpay.tb_paybillserial q WHERE p.oid_billno = q.oid_billno AND p.col_accttype = 2 AND p.col_type = 1 AND p.stat_bill = '4' AND q.pay_stat = 1 AND q.col_stat = 1 AND p.col_custid = %s AND q.date_acct = %s) as revok) D""" try: #连接MySQL数据库 connDB= MySQLdb.connect("192.168.1.6","root","root","test" ) connDB.select_db('test') curSql1 = connDB.cursor() #查询商户 curSql1.execute(sqlStr1,checkAcc_date) payCustID = curSql1.fetchall() if len(payCustID) < 1: print ('No found checkbill data,Please check the data for %s!' %checkAcc_date) exit(1) for row in payCustID: custid = row[0] #创建汇总日账单文件名称 fileName = '%s/JYMXSUM_%s_%s.csv' %(fileDir,custid,checkAcc_date) #判断文件是否存在, 如果存在则删除文件,否则生成文件! if os.path.exists(fileName): os.remove(fileName) print 'The file start generating! %s' %time.strftime('%Y-%m-%d %H:%M:%S') print '%s' %fileName #打开游标 curSql2= connDB.cursor() #执行SQL checkAcc_date = yestoday.strftime('%Y%m%d') curSql2.execute(sqlStr2,(custid,checkAcc_date,custid,checkAcc_date,custid,checkAcc_date,custid,checkAcc_date,c ustid,checkAcc_date,custid,checkAcc_date,custid,checkAcc_date,custid,checkAcc_date)) #获取数据 datesumpay = curSql2.fetchall() #打开文件 outfile = open(fileName,'w') for sumpay in datesumpay: totalNum = sumpay[0] succeedNum = sumpay[1] succeedAmt= sumpay[2] returnNum = sumpay[3] returnAmt = sumpay[4] revokeNum = sumpay[5] revokeAmt = sumpay[6] #生成汇总日账单文件 outfile.write('%s|%s|%s|%s|%s|%s|%s\n' %(totalNum,succeedNum,succeedAmt,returnNum,returnAmt,revokeNum,revo keAmt)) outfile.flush() curSql2.close() curSql1.close() connDB.close() print 'The file has been generated! %s' %time.strftime('%Y-%m-%d %H:%M:%S') except MySQLdb.Error,err_msg: print "MySQL error msg:",err_msg
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python常见数据库操作技巧汇总》、《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》
希望本文所述对大家Python程序设计有所帮助。
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2024年11月07日
2024年11月07日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]