码迷,mamicode.com
首页 > 数据库 > 详细

Python 数据库到处到Excel

时间:2014-10-18 11:03:56      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   os   ar   for   sp   

 1 import MySQLdb
 2 import xlwt
 3 def outMySQL(file_name):
 4     wb = xlwt.Workbook()
 5     sh = wb.add_sheet(sheet 1,cell_overwrite_ok=True)
 6 
 7     db = MySQLdb.connect("localhost","root","123","STUDENTS",use_unicode=1,charset=utf8)
 8     cursor = db.cursor()
 9 
10     sql = ‘‘‘SELECT * FROM STUDENTS ‘‘‘
11 
12     cursor.execute(sql)
13     data = cursor.fetchall()
14     value = cursor.description
15     rows = len(data)
16     cols = len(map(list,zip(*data)))
17     for v in range(0,len(value)):
18         sh.write(0,v,value[v][0])
19     for i in range(1,rows+1):
20         for j in range(0,cols):
21             sh.write(i,j,data[i-1][j])
22     wb.save(file_name)
23     cursor.close()
24     db.commit()
25     db.close()
26 if __name__=="__main__":
27     outMySQL(/home/wangzhch/excel.xls);

 

Python 数据库到处到Excel

标签:des   style   blog   color   io   os   ar   for   sp   

原文地址:http://www.cnblogs.com/iwangzc/p/4032672.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!