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

Python Excel导入数据库

时间:2014-10-18 11:07:50      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   ar   for   sp   数据   div   

 1 import xlrd
 2 import MySQLdb
 3 def inMySQL(file_name):
 4     wb = xlrd.open_workbook(file_name)
 5     sh = wb.sheet_by_index(0)
 6     data = [sh.row_values(row) for row in range(0,sh.nrows)]
 7 
 8     db = MySQLdb.connect("localhost","root","123","STUDENTS",use_unicode=1, charset=utf8)
 9     cursor = db.cursor()
10 
11     sql =‘‘‘INSERT INTO students VALUES(%s,%s,%s,%s,%s,%s)‘‘‘
12 
13     cursor.executemany(sql,data)
14     db.commit()
15     cursor.close()
16     db.close()
17 if __name__=="__main__":
18     inMySQL(/home/wangzhch/test.xls)

 

Python Excel导入数据库

标签:style   blog   color   os   ar   for   sp   数据   div   

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

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