mysql跨网络导出csv_mysql远程快速导出csv格式数据工具

#!/usr/bin/python

#-*- coding:UTF-8 -*-#@author Jane.Hoo

#@date2016/11/29

from__future__ import division

import os

import MySQLdb

import time

import commands

import logging

import re

import math

print'*******************************'timestamp=time.strftime("%Y%m%d%H%M%S", time.localtime())

logfile='/tmp/myloaddataout.log%s'%timestamp

logging.basicConfig(level=logging.DEBUG,

format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',

datefmt='%a, %d %b %Y %H:%M:%S',

filename='%s'%logfile,

filemode='w')

print'导出日志记录在:%s'%logfile

#################################################################################################

#定义一个StreamHandler,将INFO级别或更高的日志信息打印到标准错误,并将其添加到当前的日志处理对象#

console=logging.StreamHandler()

console.setLevel(logging.INFO)

formatter= logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')

console.setFormatter(formatter)

logging.getLogger('').addHandler(console)

#################################################################################################

#python/cygdrive/c/Users/Jane.Hoo/PycharmProjects/loaddata/MySQLloaddataout.py

db_host='127.0.0.1'db_user='test'db_pwd='test'db='test'db_port=int('3306')classToolloadout:

def __init__(self,pathfile=0):

self.pathfile=pathfile

#得到数据库连接

def getdbconn(self,DB_HOST=db_host,DB_USER='routeload',DB_PWD='routeload',DB='test01',DB_PORT='3306'):

logging.debug('%s:%s:%s:%s:%s'%(DB_HOST,DB_USER,DB_PWD,DB,DB_PORT))

conn='unlink'

try:

conn=MySQLdb.connect(host=DB_HOST,user=DB_USER,passwd=DB_PWD,port=int(DB_PORT),db=DB)

logging.info('数据库连接成功')

except MySQLdb.OperationalError,e:

logging.warning('数据库连接失败!%s'%e)returnconn

#关闭数据库连接

def closedbconn(self,conn=0):

logging.debug('闭数据库连接')

conn.close()

#检查导出文件

def chekpathfile(self):

pathfile=self.pathfile

check_flag=0iffile_exists=os.path.exists(pathfile)if iffile_exists==1:

check_flag=1logging.info('导出文件已存在!')else:

path=os.path.split(pathfile)[0]

ifpath_exists=os.path.isdir(path)if ifpath_exists==1:

logging.info('%s是有效的路径'%path)else:

check_flag=1logging.info('%s是无效的路径'%path)returncheck_flagif __name__=='__main__':

print'准备导出...'pathfile=raw_input("请输入导出文件路径:").strip()

cmd_sql=raw_input('请输入要查询的语句:').strip()

c=Toolloadout(pathfile)

pf_check=c.chekpathfile()if pf_check==0:

logging.info('文件路径校验通过')

ifinputdbmsg=raw_input('是否需要自定义数据库连接信息(Y|N)?').strip()if ifinputdbmsg=='Y':

db_host=raw_input('请输入数据地址:').strip()

db_user=raw_input('请输入用户名:').strip()

db_pwd=raw_input('请输入密码:').strip()

db=raw_input('请输入数据库名:').strip()

db_port=raw_input('请输入数据库端口:').strip()if db_host=='' or db_user=='' or db_pwd=='' or db=='':

logging.info('自定义数据库输入信息有为空.')

check_flag=1if_port=re.match(r"[0-9]", db_port)ifif_port:

db_port=int(db_port)else:

db_port=int('3306')

logging.info('端口格式输入有误,将使用默认端口%s'%db_port)

conn=c.getdbconn(db_host,db_user,db_pwd,db,db_port)if str(conn)!='unlink':

selectsql="mysql -A %s -h %s -u%s -p%s -P%s -ss -e '%s;' | sed 's/\\t/,/g;s/^//;s/$//;s/\\n//g' >%s"%(db,db_host,db_user,db_pwd,db_port,cmd_sql,pathfile)

print'querysql:',selectsqltry:

os.system('%s'%selectsql)

except BaseException,e:

logging.info('导出数据过程中报错!%s'%e)

c.closedbconn(conn)else:

logging.info('失败')else:

logging.info('文件路径校验不通过,导出结束')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值