python 导出mysql的数据表_python 导出 mysql表数据为 csv文件

# coding:utf8

import win32api

import win32con

import xlwt

import pymysql

import os

host = '127.0.0.1'

user = 'root'

pwd = '123456'

db = 'test'

sql = 'select * from master_worker'

sheet_name = '师傅信息'

DesktopPath = os.path.join(os.path.expanduser('~'),"Desktop")

out_path = DesktopPath + '\师傅信息.csv'

# print(out_path)

def export():

"""导出数据"""

conn = pymysql.connect(host,user,pwd,db,charset='utf8')

cursor = conn.cursor()

count = cursor.execute(sql)

print(count)

cursor.scroll(0,mode='absolute')

results = cursor.fetchall()

fields = cursor.description

workbook = xlwt.Workbook()

sheet = workbook.add_sheet(sheet_name,cell_overwrite_ok=True)

for field in range(0,len(fields)):

sheet.write(0,field,fields[field][0])

# row = 1

# col = 0

for row in range(1,len(results)+1):

# print(results[row - 1][1])

for col in range(0,len(fields)):

print(results[row - 1][col])

sheet.write(row,col,u'%s'%results[row-1][col])

# sheet.write(row,col,results[row-1][col])

workbook.save(out_path)

try:

export()

win32api.MessageBox(0, "导出数据 成功", "提示", win32con.MB_OK)

except Exception as e:

print(e)

win32api.MessageBox(0, "导出数据 失败\n%s"%e, "提示", win32con.MB_OK)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值