python从数据库导出数据到excel

最近需要从数据库里导出一些数据到excel,刚开始我是使用下面的命令

select * from xxx where xxx into outfile 'xxx.xls'

结果报错

ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement

除了这个错误,可能还会报access deny的错误,关于access deny的错误,有一篇博客讲的很详细:https://daizj.iteye.com/blog/2174972

请教同事后发现他们都是用脚本来导出数据,这样安全又方便,于是自己也整了一个,记录一下

1)安装easy_install

由于公司使用的是老旧的python2,所以还是使用easy)install来安装包,如果没有安装easy_install,需要安装easy_install

yum install python-setuptools

2)安装pymysql和xlwt

使用easy_install安装pymysql和xlwt

easy_install pymysql
easy_install xlwt

使用pip也可以安装

pip install pymysql
pip isntall xlwt

使用pip安装如果报错

Fatal error in launcher: Unable to create process using '"C:\Python27\python.exe" "C:\Python27\Scripts\pip.exe" install pymysql'

可以将在前面加上python2 -m (启动python环境)

python2 -m pip install pymysql

3)编写脚本

# --*-- coding:utf8 --*--
import pymysql, xlwt def export_excel(table_name):
# 连接数据库,查询数据
host, user, passwd, db='127.0.0.1','root','123','xxx'
conn = pymysql.connect(user=user,host=host,port=3306,passwd=passwd,db=db,charset='utf8')
cur = conn.cursor()
sql = 'select * from %s' % table_name
cur.execute(sql) # 返回受影响的行数 fields = [field[0] for field in cur.description] # 获取所有字段名
all_data = cur.fetchall() # 所有数据 # 写入excel
book = xlwt.Workbook()
sheet = book.add_sheet('sheet1') for col,field in enumerate(fields):
sheet.write(0,col,field) row = 1
for data in all_data:
for col,field in enumerate(data):
sheet.write(row,col,field)
row += 1
book.save("%s.xls" % table_name) if __name__ == '__main__':
export_excel('app1_book')

4)启动脚本导出数据

python2 export.py

使用python脚本从数据库导出数据到excel的更多相关文章

  1. 从数据库导出数据到excel之List<List<Object>>导出

    说明:有时候数据处理为List<List<Object>>更方便 姊妹篇:从数据库导出数据到excel之List<Map<>>导出 兄弟篇:从数据库导出 ...

  2. 从数据库导出数据到excel之POI操作

    项目说明: 1:数据库中有两张表,主键关联 2:根据条件查询数据 3:处理为需要的数据封装类型,然后传到导出excel的方法中 <--框架部署就不详谈了,用的spring框架--> 补充: ...

  3. 数据库导出数据到excel格式

    场景: 由于业务人员经常会找DBA导出一些数据,写了一个自动导出脚本. import pymysql from openpyxl import Workbook from openpyxl.write ...

  4. VBA中数据库导出数据到Excel注意事项

    Sub ReadDBData() On Error GoTo ErrorHand Dim dbHelper As New dbHelper Dim sqlSQL As String Dim rs As ...

  5. 从数据库导出数据到excel之List<map>导出

    说明:很多时候取出来的数据是封装为List<Map<String,Object>>,可以直接导出excel表格 项目说明就在 “上一篇” 直接上代码(数据层和业务层不用说了,查 ...

  6. 使用shell从DB2数据库导出数据

    使用shell脚本根据输入的用户名,数据库名,密码从DB2数据库导出数据 (1)a.sh脚本如下 #!/usr/bin/bash read -p "please input your DBN ...

  7. python 导出数据到excel 中,一个好用的导出数据到excel模块,XlsxWriter

    最近公司有项目需要导出数据到excel,首先想到了,tablib,xlwt,xlrd,xlwings,win32com[还可以操作word],openpyxl,等模块但是 实际操作中tablib 写入 ...

  8. Python导出数据到Excel表格-NotImplementedError: formatting_info=True not yet implemented

    在使用Python写入数据到Excel表格中时出现报错信息记录:“NotImplementedError: formatting_info=True not yet implemented” 报错分析 ...

  9. 微软BI 之SSIS 系列 - 导出数据到 Excel 2013 的实现

    开篇介绍 碰到有几个朋友问到这个问题,比较共性,就特意写了这篇小文章说明一下如何实现在 SSIS 中导出数据到 Office Excel 2013 中.通常情况下 2013 以前的版本大多没有问题,但 ...

随机推荐

  1. vsftpd下载文件时内容乱码

    windows客户端访问Linux服务端的ftp并下载文档时,内容会出现乱码,这是由于vsftpd文件服务器不支持转码功能 通过java FTPClient下载 方法为 OutputStream is ...

  2. 【Python】【demo实验32】【回文数的确认】

    原题: 我的代码: #!/usr/bin/python # encoding=utf-8 # -*- coding: UTF-8 -*- #判断一个数字是否为回文数 即 12345654321 x = ...

  3. MFC控件使用大全

    https://blog.csdn.net/daoming1112/article/details/54698113

  4. PAT B1026.程序运行时间

    AC代码 #include <cstdio> #define CLK_TCK 100 int main() { int C1, C2, C3; scanf("%d%d" ...

  5. RMQ+差分处理(Let Them Slide)Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)

    题意:https://codeforc.es/contest/1208/problem/E 现有n行w列的墙,每行有一排连续方块,一排方块可以左右连续滑动,且每个方块都有一个价值,第i 列的价值定义为 ...

  6. JAVA汽车4S店管理系统

    JAVA汽车4S店管理系统源码(前台+后台)分为这5个大模块 系统设置 整车销售辅助销售汽修管理 汽修统计1.经理管理(增加 和删除功能)    表设计经理编号经理名年龄性别2.业务员管理(增删改查) ...

  7. 基于Springboot后台,前台 vue.js 跨域 Activiti6 工作流(用到websocket技术) 的项目

    工作流模块----------------------------------------------------------------------------------------------- ...

  8. Swoft 2.0.3 重大更新,发布优雅的微服务治理

     什么是 Swoft ? Swoft 是一款基于 Swoole 扩展实现的 PHP 微服务协程框架.Swoft 能像 Go 一样,内置协程网络服务器及常用的协程客户端且常驻内存,不依赖传统的 PHP ...

  9. T100-----汇出EXCEL表格

    例子:cxmp541 #excel匯出功能 ON ACTION exporttoexcel LET g_action_choice="exporttoexcel" IF cl_au ...

  10. Mysql-Sqlalchemy-多表操作

    import sqlalchemy from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declar ...