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. vue60秒倒计时

    wait:"60", content:"验证码", canClick: true, daojishi(){ if(!this.canClick) return ...

  2. Reaching Points

    A move consists of taking a point (x, y) and transforming it to either (x, x+y) or (x+y, y). Given a ...

  3. springboot整合httpClient

    创建httpClientConfig配置类 @Configuration @PropertySource(value="classpath:/properties/httpClient.pr ...

  4. SpringBoot 初入门

    SpringBoot 初入门 关于介绍什么之类的就不讲了,主要做一下学习记录. 1. 启动方式 IDEA 启动 命令行启动: mvn spring-boot:run 部署到服务器启动: 先进行打包, ...

  5. 21-Perl 发送邮件

    1.Perl 发送邮件如果你的程序在 Linux/Unix 系统上运行,你就可以在 Perl 中使用 sendmail 工具来发送邮件.以下是一个简单的脚本实例用于发送邮件:#!/usr/bin/pe ...

  6. C#取绝对值函数

    System.Math.Abs(float value); System.Math.Abs(decimal value); System.Math.Abs(int value); System.Mat ...

  7. O061、Boot from Volume

    参考https://www.cnblogs.com/CloudMan6/p/5679384.html   Volume 除了可以用作Instance的数据盘,也可以作为启动盘(Bootable Vol ...

  8. LeetCode 腾讯精选50题--有效的括号

    根据题意,第一反应就是使用栈,左右括号相匹配,则将左括号出栈,否则将左括号入栈. 这里我用数组配合“指针”模拟栈的入栈与出栈操作,初始时指针位置指向0,表示空栈,凡遇上左括号则直接入栈,若遇上有括号, ...

  9. 深入理解hadoop数据倾斜

    深入理解hadoop之数据倾斜 1.什么是数据倾斜 我们在用map /reduce程序执行时,有时候会发现reduce节点大部分执行完毕,但是有一个或者几个reduce节点运行很慢,导致整个程序的处理 ...

  10. Java并发编程——线程的基本概念和创建

    一.线程的基本概念: 1.什么是进程.什么是是线程.多线程? 进程:一个正在运行的程序(程序进入内存运行就变成了一个进程).比如QQ程序就是一个进程. 线程:线程是进程中的一个执行单元,负责当前进程中 ...