# -*- codeing = utf-8 -*- # @Time : 2021/5/15 12:55 # @Author : Clown # @File : mysql导入excel文件.py # @Software : PyCharm import pymysql import xlrd import os import time import hashlib # 连接数据库 try: db = pymysql.connect(host="localhost", user="root", passwd="111???hxx", db="zuzu_data", charset='utf8mb4') print('connect to mysql server 成功') print('------------------------------------') except: print("could not connect to mysql server") def open_excel(file): try: book = xlrd.open_workbook(file) # 文件名,把文件与py文件放在同一目录下 except: print("open excel file failed!") try: sheet = book.sheet_by_name("Sheet1") # execl里面的worksheet1 return sheet except: print("locate worksheet in excel failed!") # # def insert_deta(file): sheet = open_excel(file) cursor = db.cursor() row_num = sheet.nrows for i in range(1, row_num): # 第一行是标题名,对应表中的字段名所以应该从第二行开始,计算机以0开始计数,所以值是1 row_data = sheet.row_values(i) value = (row_data[0], row_data[1], row_data[2], row_data[3], row_data[4], row_data[5], row_data[6], row_data[7], row_data[8], row_data[9], row_data[10], row_data[11], row_data[12], row_data[13], row_data[14], row_data[15], str(int(float(row_data[16]))), row_data[17], str(int(float(row_data[18]))), row_data[19], row_data[20], row_data[21], row_data[22], row_data[23], row_data[24], row_data[25], row_data[26], row_data[27]) per = i/(row_num-1)*100 print('\r 已导入%s行,共计%s行,已完成%2d%%'%(str(i),str(row_num-1),per),end='') sql = "INSERT INTO order_forms_zzx VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" #('日期','订单编号','下单时间','商品原价','包装费','配送费','订单原价','顾客实付','商家实收','商家总活动支出','佣金','用户ID','用户地址经纬度','配送时长','平台','城市','食亨门店id','食亨门店名称','平台门店id','平台门店名称','1级组织','2级组织','3级组织','4级组织','5级组织','6级组织','7级组织','8级组织') cursor.execute(sql, value) # 执行sql语句 db.commit() cursor.close() # 关闭连接 def insert_deta1(file): sheet = open_excel(file) cursor = db.cursor() row_num = sheet.nrows for i in range(1, row_num): # 第一行是标题名,对应表中的字段名所以应该从第二行开始,计算机以0开始计数,所以值是1 n = (str(i)).zfill(10) time_str = str(int(time.time())) + n # print(time_str) row_data = sheet.row_values(i) # 定义hash值 md5 = hashlib.md5() md5.update(row_data[3].encode('utf-8')) # 将【标准商品名称】改为hash值 value = (time_str,row_data[0], row_data[1], md5.hexdigest(), row_data[3], row_data[4], row_data[5], row_data[6], row_data[7], row_data[8], str(int(float(row_data[9]))), row_data[10], str(int(float(row_data[11]))), row_data[12], row_data[13], row_data[14], row_data[15], row_data[16], row_data[17], row_data[18], row_data[19], row_data[20]) per = i / (row_num - 1) * 100 print('\r 已导入%s行,共计%s行,已完成%2d%%'%(str(i),str(row_num-1),per),end='') sql = "INSERT INTO order_formsgoods_zzx VALUES(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)" #('日期','订单编号','下单时间','商品原价','包装费','配送费','订单原价','顾客实付','商家实收','商家总活动支出','佣金','用户ID','用户地址经纬度','配送时长','平台','城市','食亨门店id','食亨门店名称','平台门店id','平台门店名称','1级组织','2级组织','3级组织','4级组织','5级组织','6级组织','7级组织','8级组织') cursor.execute(sql, value) # 执行sql语句 db.commit() cursor.close() # 关闭连接 # # if __name__ == '__main__': file_name = r'/home/python_flies/orderFormsSplit_goods_zxx'# 订单详情明细z 、订单详情z for a,b,files in os.walk(file_name,topdown = False): for file in files: path = str(file_name)+'/'+str(file) print('%s 正在导入Mysql'%str(file)) open_excel(path) insert_deta1(path)#订单详情明细insert_deta1、订单详情insert_deta print('\n %s 导入Mysql完毕'%str(file)) print('------------------------------------') file_name = r'/home/python_flies/orderFormsSplit_forms_zxx'# 订单详情明细z 、订单详情z for a,b,files in os.walk(file_name,topdown = False): for file in files: path = str(file_name)+'/'+str(file) print('%s 正在导入Mysql'%str(file)) open_excel(path) insert_deta(path)#订单详情明细insert_deta1、订单详情insert_deta print('\n %s 导入Mysql完毕'%str(file)) print('------------------------------------')