用python脚本导出mysql数据库查询结果到Excel表

最近需要导数据的情况太多,总用跳板机上的navicat工具有点效率低,也觉得挺麻烦的(由于跳板机无法连通外网 所以导出数据文件还得通过sftp传到本机)
anyway 还是写个脚本好了。之前写过一个shell脚本做的定时导出任务,现在试试用python写下

主要用到的库有:
pymysql -- 连数据库的无需多说
os & sys -- 可能回涉及到工作目录或者外部传参
xlwt -- 写excel

下面就是代码了,总体还是挺简单的,主要遇到个字符编码的问题,改成utf-8就解决了
还有个没解决掉的问题: select的结果没有字段名,所以....文件第一行的字段我是手动加进去的 =.=

#!/usr/bin/python
# -*- coding: cp936 -*-
# security:数据库相关的敏感参数最好通过传参形式传入而非写入脚本

import pymysql
import xlwt
import os,sys

def dbConnect(dburl):
    db_user = sys.argv[1]
    db_pass = sys.argv[2]
    db_name = raw_input("要使用的数据库:")
    db_conn = pymysql.connect(dburl,db_user, db_pass, db_name)# db_name传参失败?编码问题"?"
    return db_conn

def sqlOpt(opt):
    db_conn = dbConnect("localhost")
    cur     = db.cursor()
    cur.execute(opt)
    data    = cur.fetchall()
    db_conn.close()
    return data

def write_to_excel(content):
    os.chdir("/home/appuser/export_mysql_data/")
    filename   = raw_input("请输入要保存的文件名,无需后缀:") + '.xls'
    wbk        = xlwt.Workbook(encoding='utf-8')
    test       = wbk.add_sheet('test',cell_overwrite_ok=True)
    # ?如何获取列名
    fileds     = [u'ID',u'name',u'sex',u'birth',u'department',u'address']
    trans_data = list(content)
    # 写入列名
    for filed in range(0,len(fileds)):
        test.write(0,filed,fileds[filed])
    for row in range(1,len(trans_data)+1):
        for col in range(0,len(fileds)):
            test.write(row,col,str(trans_data[row-1][col]))
    wbk.save(filename)

def run_Task():
    sql    = raw_input("请输入sql查询语句:")
    result = sqlOpt(sql)
    write_to_excel(result)

run_Task()

----------------------------------------------------------------分割线----------------------------------------------------------------------------
11.1更
解决了手动输入列名的问题,代码更改如下:

import pymysql
import xlwt
import os
# import pandas as pd

# database conn
def dbConnect(dburl):
    db_user = 'developer'
    db_pass = '1qaz2wsx#EDC'
    # db_name = input("要使用的数据库:")
    db_conn = pymysql.connect(dburl, db_user, db_pass)  
    return db_conn

#获取列名
def getCol(opt):
    db_conn = dbConnect("10.1.188.241")
    cursor  = db_conn.cursor()
    cursor.execute(opt)
    col = tuple([tuple[0] for tuple in cursor.description])
    db_conn.close()
    return col

#获取数据
def sqlOpt(opt):
    db_conn = dbConnect("10.1.188.241")
    cursor  = db_conn.cursor()
    cursor.execute(opt)
    data = cursor.fetchall()
    # final_data = col + data
    db_conn.close()
    return data

# 写入文件
def write_into_excel(col_name, content):
    os.chdir("D:\whj")
    filename   = input("请输入要保存的文件名,无需后缀:") + '.xls'
    wbk        = xlwt.Workbook(encoding='utf-8')
    test       = wbk.add_sheet('test', cell_overwrite_ok=True)

    fileds     = list(col_name)
    trans_data = list(content)
    # 写入列名
    for filed in range(0, len(fileds)):
        test.write(0, filed, fileds[filed])
    for row in range(1, len(trans_data)+1):
        for col in range(0, len(fileds)):
            test.write(row, col, u'%s' % str(trans_data[row-1][col]))
    wbk.save(filename)

# 主函数
def run_Task():
    sql = "select name,phone from wechat.sys_user;"
    result = sqlOpt(sql)
    col_name = getCol(sql)
    # print(result)
    # print(col_name)
    write_into_excel(col_name, result)

run_Task()

转载于:https://blog.51cto.com/healerj9/2306479

你可能感兴趣的:(用python脚本导出mysql数据库查询结果到Excel表)