Python实现将MySQL数据库表中的数据导出生成csv格式文件的方法


Posted in Python onJanuary 11, 2018

本文实例讲述了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 开发的三种运行模式详细介绍
Jan 18 Python
Python实现查找匹配项作处理后再替换回去的方法
Jun 10 Python
python3.0 模拟用户登录,三次错误锁定的实例
Nov 02 Python
python Django框架实现web端分页呈现数据
Oct 31 Python
python代码xml转txt实例
Mar 10 Python
Python openpyxl 插入折线图实例
Apr 17 Python
Selenium之模拟登录铁路12306的示例代码
Jul 31 Python
Python tempfile模块生成临时文件和临时目录
Sep 30 Python
Django配置Bootstrap, js实现过程详解
Oct 13 Python
详解非极大值抑制算法之Python实现
Jun 28 Python
Python装饰器的练习题
Nov 23 Python
Python正则表达式中flags参数的实例详解
Apr 01 Python
python+django+sql学生信息管理后台开发
Jan 11 #Python
hmac模块生成加入了密钥的消息摘要详解
Jan 11 #Python
快速了解Python中的装饰器
Jan 11 #Python
简单了解python模块概念
Jan 11 #Python
100行Python代码实现自动抢火车票(附源码)
Jan 11 #Python
python实现外卖信息管理系统
Jan 11 #Python
Python实现学生成绩管理系统
Apr 05 #Python
You might like
example2.php
2006/10/09 PHP
判“新”函数:得到今天与明天的秒数
2006/10/09 PHP
php不用内置函数对数组排序的两个算法代码
2010/02/08 PHP
php错误、异常处理机制(补充)
2012/05/07 PHP
PHP中常用的输出函数总结
2014/09/22 PHP
php连接mysql数据库最简单的实现方法
2019/09/24 PHP
Jquery中getJSON在asp.net中的使用说明
2011/03/10 Javascript
关于jQuery object and DOM element
2013/04/15 Javascript
JavaScript弹出窗口方法汇总
2014/08/12 Javascript
使用script的src实现跨域和类似ajax效果
2014/11/10 Javascript
JQuery设置时间段下拉选择实例
2014/12/30 Javascript
使用vue.js实现联动效果的示例代码
2017/01/10 Javascript
Angular 2父子组件数据传递之@Input和@Output详解(下)
2017/07/05 Javascript
对于js垃圾回收机制的理解
2017/09/14 Javascript
通过js给网页加上水印背景实例
2019/06/17 Javascript
在Python中使用sort()方法进行排序的简单教程
2015/05/21 Python
Python之父谈Python的未来形式
2016/07/01 Python
Python中生成器和迭代器的区别详解
2018/02/10 Python
python hook监听事件详解
2018/10/25 Python
python 下 CMake 安装配置 OPENCV 4.1.1的方法
2019/09/30 Python
Pandas实现DataFrame按行求百分数(比例数)
2019/12/27 Python
python如何调用百度识图api
2020/09/29 Python
html5读取本地文件示例代码
2014/04/22 HTML / CSS
HTML5 Canvas实现图片缩放、翻转、颜色渐变的代码示例
2016/02/28 HTML / CSS
英国经济型酒店品牌:Travelodge
2019/12/17 全球购物
Unix如何在一行中运行多个命令
2015/05/29 面试题
自荐信模版
2013/10/24 职场文书
群众路线剖析材料怎么写
2014/10/09 职场文书
幼儿园新生开学寄语
2015/05/27 职场文书
2015年暑假生活总结
2015/07/13 职场文书
2015年幼师个人工作总结
2015/10/15 职场文书
承诺书怎么写 ?
2019/04/16 职场文书
MySQL不使用order by实现排名的三种思路总结
2021/06/02 MySQL
React 并发功能体验(前端的并发模式)
2021/07/01 Javascript
深入浅析Django MTV模式
2021/09/04 Python
Java使用Unsafe类的示例详解
2021/09/25 Java/Android