mysql怎么导出表结构及数据到csv文件

发布网友 发布时间:2022-04-24 18:41

我来回答

2个回答

懂视网 时间:2022-04-07 22:32

转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/45841221

#!/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("rdsq99sy52dvv160hz76w.mysql.rds.aliyuncs.com","sysdba","UubeeDBmydb1","dbchkbill" )
    connDB.select_db(‘dbchkbill‘)
    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 ‘ %(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脚本---把MySQL数据库表中的数据导出生成csv格式文件

标签:

热心网友 时间:2022-04-07 19:40

  mysql导出数据库到csv文件的方法:
  1、通过mysql客户端shell连接到服务器,选择使用的数据库,输入sql代码:

  select * from test_info
into outfile '/tmp/test.csv'
fields terminated by ',' optionally enclosed by '"' escaped by '"'
lines terminated by '\r\n';
  里面最关键的部分就是格式参数
  这个参数是根据RFC4180文档设置的,该文档全称Common Format and MIME Type for Comma-Separated Values (CSV) Files,其中详细描述了CSV格式,其要点包括:
  (1)字段之间以逗号分隔,数据行之间以\r\n分隔;
  (2)字符串以半角双引号包围,字符串本身的双引号用两个双引号表示。
  通过执行上述代码,便可以将需要的数据以csv格式导出到执行的文件中。
  
  2、另外,MySQL中导入CSV格式数据的sql代码如下:
  load data infile '/tmp/test.csv'
into table test_info
fields terminated by ',' optionally enclosed by '"' escaped by '"'
lines terminated by '\r\n';

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com