使用Python从 MySQL写数据到Excel

直接上代码:

#!/usr/bin/env python
#coding:utf-8

import xlwt
import MySQLdb
import datetime

database = MySQLdb.connect(host=‘192.168.1.30‘,user=‘root‘,passwd=‘123456‘,db=‘crm‘)
#设置字符集
database.set_character_set(‘utf8‘)
cursor = database.cursor()
cursor.execute(‘SET NAMES utf8;‘) 
cursor.execute(‘SET CHARACTER SET utf8;‘)
cursor.execute(‘SET character_set_connection=utf8;‘)

starttime = datetime.datetime.now()
print ‘开始时间:%s‘ % (starttime)

#通过SQL得到该表有多少行,如果想取出指定的数据,只需要在后面加where条件即可。
sql2 = ‘select count(*) from bill_test;‘;
cursor.execute(sql2)
count_rows=cursor.fetchone()[0]

wbk = xlwt.Workbook(encoding=‘utf-8‘,style_compression=0)
sheet = wbk.add_sheet(‘sheet 1‘, cell_overwrite_ok=True)
#设置写excel的样式
style = xlwt.XFStyle()
font = xlwt.Font()
font.name = ‘Times New Roman‘
#0x0190设置字体为20,默认为0x00C8 字体为10 ,0x00C8为十六进制的数字
font.height = 0x0190
font.bold = True
style.font = font
#查询得到该表有多少列
query_colums="select count(*) from information_schema.COLUMNS where TABLE_SCHEMA=‘crm‘ and table_name=‘bill_test‘;"
cursor.execute(query_colums)
count_cols = cursor.fetchone()[0]

sql = ‘select member_id, name, tel, phone, dq_datetime, address, parking from bill_test;‘
cursor.execute(sql)

#定义所有的列名,共7列
columnName = [‘账号‘,‘名称‘,‘电话‘,‘手机‘,‘到期日期‘,‘地址‘,‘园区名称‘]  
#将列名插入表格,共7列
for i in range(len(columnName)):         
        sheet.write(0,i,columnName[i],style)

#通过循环取出每一行数据,写入excel    
for i in range(1,count_rows-1):
    data = cursor.fetchone()
    for j in range(0,count_cols-1):
        sheet.write(i,j,data[j],style)       
cursor.close()
database.close()
wbk.save(‘C:\Users\XUWU\Desktop\data01.xls‘)   

endtime=datetime.datetime.now()
print ‘结束时间:%s‘ % (endtime)
print ‘用时:%s 秒‘ % (endtime-starttime)

执行情况:

时间: 2024-08-05 08:41:23

使用Python从 MySQL写数据到Excel的相关文章

python从mysql导出数据导excel

# coding:utf8 import sys reload(sys) sys.setdefaultencoding('utf8') # author: 'zkx' # date: '2018/3/11' # Desc:从数据库中导出数据到excel数据表中 #已封装,可以直接使用,只需更改sql语句即可 import xlwt import MySQLdb def export(host,user,password,dbname,table_name,outputpath): conn =

mysql 写数据操作几次硬盘?

mysql 写数据步骤: 1:写入操作事物日志,持久化操作日志到磁盘,并且只是写在磁盘上一小块区域内的顺序io,不需要像随机io一样 在磁盘多个地方移动磁头 2:内存中事物日志持久化以后  ,写入的数据 ,更新到内存 3: 内存中被修改的数据在后台再持久化到磁盘

python 使用openpyxl来写数据到excel表格

使用openpyxl写execl确实很方便.我先介绍用到的相关模块与函数 Workbook:工作簿模块,在内存创建一个工作簿. ExcelWriter:使用它向exel中写数据. get_column_letter:给一个数字得到一个列名,如A,B,C 数据写入到EXCEL表格 #!/usr/bin/env python # _*_ coding:utf-8 _*_ from openpyxl.workbook import Workbook from openpyxl.writer.excel

python连接mysql获取数据 字符串 获取变量

python脚本中的变量经常会变动,所以考虑写到mysql里面如何获取mysql里面数据作为参数,参考如下脚本: #!/usr/bin/python # -*- coding: utf-8 -*- import MySQLdb # 打开数据库连接 db = MySQLdb.connect("3.12.5.1", "root", "root", "test", charset='utf8') # 使用cursor()方法获取操作

使用Python采集web质量数据到Excel表

众所周知,Python有很对第三方模块,只要熟练应用这些模块即可完成各种任务,在开始采集数据之前需要存在一个目标站点,然后使用Python脚本进行质量数据采集.探测web质量需要用到Python的pycurl模块,它可以获取HTTP请求的状态码,DNS解析时间.建立连接时间.传输结束总时间,下载数据包大小,HTTP头部大小.平均下载速度等参数.从这些参数中可以了解web的服务质量如何,然后进行优化等操作.将获取的数据写到Excel表格中,这里使用的是Python的xlsxwrite模块,实现的思

13.5.1 写数据到 Excel

Excel 的互操作程序集都是标准的 .NET 程序集,我们可以在 F# Interactive 中使用 #r 指令进行引用.引用程序集之后,就能使用类了,既可以把Excel 作为独立(可见或不可见)的应用程序运行,也可以写脚本.清单 13.18 演示了如何启动 Excel,创建有一个工作表的新工作簿,以及把数据写入工作表. 清单13.18 启动 Excel 创建工作表 (F#) #r "office.dll" #r "Microsoft.Office.Interop.Exc

python向mysql插入数据一直报TypeError: must be real number,not str

注意,Python向MySQL中写入数据时无论输入的数据类型如何,语句中的占位符均使用%s,例如 这里的price我是int类型的,所以占位符用的%d,后来改成float类型,占位符改为%f,都不可以!!!! 也就是无论输入的数据是否为字符串,占位符都是%s,不存在%f,%d这种概念. 原文地址:https://www.cnblogs.com/qilin20/p/12111218.html

[ES]Python查询ES导出数据为Excel

版本 elasticsearch==5.5.0 python==3.7 说明 用python查询es上存储的状态数据,将查询到的数据用pandas处理成excel code # -*- coding: utf-8 -*- # @Time : 2019/7/22 10:41 # @Author : Skyell Wang # @FileName: es_data_get.py from elasticsearch import Elasticsearch import pandas as pd i

python 插入mysql数据库数据

建立数据库连接 def create_db_connect():    """    brief info for: create_db_connect         建立数据库链接          Args:          Return:          Raise:    """    conn = MySQLdb.connect(host = "rm-uf6wz3f7kb8sx983zo.mysql.rds.aliyun