使用python脚本从数据库导出数据到excel
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的更多相关文章
- 从数据库导出数据到excel之List<List<Object>>导出
说明:有时候数据处理为List<List<Object>>更方便 姊妹篇:从数据库导出数据到excel之List<Map<>>导出 兄弟篇:从数据库导出 ...
- 从数据库导出数据到excel之POI操作
项目说明: 1:数据库中有两张表,主键关联 2:根据条件查询数据 3:处理为需要的数据封装类型,然后传到导出excel的方法中 <--框架部署就不详谈了,用的spring框架--> 补充: ...
- 数据库导出数据到excel格式
场景: 由于业务人员经常会找DBA导出一些数据,写了一个自动导出脚本. import pymysql from openpyxl import Workbook from openpyxl.write ...
- VBA中数据库导出数据到Excel注意事项
Sub ReadDBData() On Error GoTo ErrorHand Dim dbHelper As New dbHelper Dim sqlSQL As String Dim rs As ...
- 从数据库导出数据到excel之List<map>导出
说明:很多时候取出来的数据是封装为List<Map<String,Object>>,可以直接导出excel表格 项目说明就在 “上一篇” 直接上代码(数据层和业务层不用说了,查 ...
- 使用shell从DB2数据库导出数据
使用shell脚本根据输入的用户名,数据库名,密码从DB2数据库导出数据 (1)a.sh脚本如下 #!/usr/bin/bash read -p "please input your DBN ...
- python 导出数据到excel 中,一个好用的导出数据到excel模块,XlsxWriter
最近公司有项目需要导出数据到excel,首先想到了,tablib,xlwt,xlrd,xlwings,win32com[还可以操作word],openpyxl,等模块但是 实际操作中tablib 写入 ...
- Python导出数据到Excel表格-NotImplementedError: formatting_info=True not yet implemented
在使用Python写入数据到Excel表格中时出现报错信息记录:“NotImplementedError: formatting_info=True not yet implemented” 报错分析 ...
- 微软BI 之SSIS 系列 - 导出数据到 Excel 2013 的实现
开篇介绍 碰到有几个朋友问到这个问题,比较共性,就特意写了这篇小文章说明一下如何实现在 SSIS 中导出数据到 Office Excel 2013 中.通常情况下 2013 以前的版本大多没有问题,但 ...
随机推荐
- spring-boot 环境搭建(一)
环境 jdk 8 tomcat 8.5 sts 4.4.2 maven 3.6.1 新建 maven 项目 首先创建一个普通的 maven 项目. pom.xml <project xmlns= ...
- Spring4学习回顾之路08- FactoryBean配置Bean
建立Student.java package com.lql.srping04; /** * @author: lql * @date: 2019.10.28 * Description: */ pu ...
- 网站如何接入微信公众号JSAPI支付PHP版
1.首先,我们要有一个微信公众号(分类类型有订阅号,服务号,企业号)我们的微信公众号一定是个服务号只有它才有微信支付接口.. 并且这个微信公众号一定要进行微信认证才能申请微信支付接口. 2.申请JSA ...
- git链接远程库
码云版本库使用流程 生成公钥 ssh-keygen -t rsa -c "码云申请邮箱" 添加公钥到本地 ssh-agent bash ssh-add ~/.ssh/id_rsa ...
- web框架链接
django系列教程(优):https://www.cnblogs.com/feixuelove1009/p/5823135.html
- mysql45讲
一共48讲,学习时间2天. 开篇词 (1讲) 开篇词 | 这一次,让我们一起来搞懂MySQL 理论指导实践:先系统性的学习原理,再实践验证. 你可以从点到线再到面,形成自己到mysql知识网络. 在使 ...
- 8-Perl 哈希
1.Perl 哈希哈希是 key/value 对的集合.Perl中哈希变量以百分号 (%) 标记开始.访问哈希元素格式:${key}.以下是一个简单的哈希实例:#!/usr/bin/perl%data ...
- Python(六) —— 网络请求
接口调用 接口调用有几个模块可以用:urllib 和 requests ,urllib 是内置的模块,极其不好用,强烈推荐用 requests 模块 get 请求 1.普通的 get 请求 impor ...
- 一行python能干什么?
我们都知道,python作为一个编程语言,它有一个最大的优势就是代码简短,那么一行python代码能实现哪些操作呢?一起来看看吧! 1.打印Hello World! 这是最基础的,相信不管学习哪一门语 ...
- 转载Linux常用命令
转自:https://blog.csdn.net/deng_xj/article/details/88803148 Linux常用shell命令 [root@dengxj]#各项含义[用户名@计算机名 ...