Python Excel导入数据库

 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')

 

posted @ 2014-10-18 10:33  王志超i  阅读(342)  评论(0编辑  收藏  举报