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

 

你可能感兴趣的:(Python Excel导入数据库)