如何将mysql导入到excel,今天我们就讲讲简单的将excel数据导入到mysql
python是使用模块xlrd来导入到mysql的,使用pymysql(python2.X的使用MySQLdb)来链接mysql数据库的
-------------代码-------------------------
import xlrd
import pymysql
_host = 'localhost'
_db = '13net'
_user = 'root'
_password = 'root'
_table = 'net_members_bak'
_excel_name = './tes2t.xlsx'
excel = xlrd.open_workbook(_excel_name)
sheet = excel.sheet_by_index(0)
rows = sheet.nrows
cols = sheet.ncols
data = []
fields=''
for i in range(1,rows):
data.append(sheet.row_values(i))
for i in range(0,cols):
fields = fields+'%s,'
print(fields)
conn = pymysql.connect(host=_host,user=_user,password=_password,db=_db,charset='utf8')
cursor = conn.cursor()
cursor.executemany("insert into "+_table+" values("+fields[:-1]+");" ,data)
conn.commit()
-------------end-------------------------
总的来说python在mysql=====excel数据的导入导出的的确确是要比php简单易懂很多,还是要多学学其他语言,才能看到更好的方法