python 读取excel写入mysql

import pymysql,time,datetime
from openpyxl import load_workbook


# 建立连接
db=pymysql.Connect(host="localhost",port=3306,user="tb",passwd="tb123456",db="tb",charset="utf8")
#建立游标
cursor=db.cursor()

#读取
wb = load_workbook(filename=r'C:\Users\PC\Desktop\excel\8月.xlsx')
sheet = wb['Shell1']
# print(sheet['A4'].value)

for i in range(2,sheet.max_row):
    #日期
    # today = datetime.now.strftime("%Y-%m-%d")
    today = datetime.date.today()

    K_CODE = str(sheet['B%s'%i].value)
    K_NAME = str(sheet['C%s'%i].value)
    SHOP_NAME = str(sheet['A%s'%i].value)
    SHOP_SUM = str(sheet['F%s'%i].value)
    print('写入%s' % SHOP_NAME)
    # 写入内容
    # sql = "insert into shopinfo(K_CODE,K_NAME,SHOP_NAME,SHOP_SUM,Y_TIME) values('"+K_CODE+"','"+K_NAME+"','"+SHOP_NAME+"','"+SHOP_SUM+"','"+today+"')"
    # sql = "insert into shopinfo(K_CODE,K_NAME,SHOP_NAME,SHOPSUM) values('"sheet['B%s'%i].value","sheet['C%s'%i].value","sheet['A%s'%i].value","sheet['F%s'%i].value"')"
    sql = "insert into jdinfo(K_CODE,K_NAME,SHOP_NAME,SHOP_SUM,Y_TIME) values('{}','{}','{}','{}','{}')" .format(K_CODE,K_NAME,SHOP_NAME,SHOP_SUM,today)
    cursor.execute(sql)
    db.commit()

print('写入完成')

sheet = wb['Shell2']
# print(sheet['A4'].value)

for i in range(2,sheet.max_row):
    #日期
    # today = datetime.now.strftime("%Y-%m-%d")
    today = datetime.date.today()

    K_CODE = str(sheet['B%s'%i].value)
    K_NAME = str(sheet['C%s'%i].value)
    SHOP_NAME = str(sheet['A%s'%i].value)
    SHOP_SUM = str(sheet['F%s'%i].value)
    print('写入%s' % SHOP_NAME)
    # 写入内容
    # sql = "insert into shopinfo(K_CODE,K_NAME,SHOP_NAME,SHOP_SUM,Y_TIME) values('"+K_CODE+"','"+K_NAME+"','"+SHOP_NAME+"','"+SHOP_SUM+"','"+today+"')"
    # sql = "insert into shopinfo(K_CODE,K_NAME,SHOP_NAME,SHOPSUM) values('"sheet['B%s'%i].value","sheet['C%s'%i].value","sheet['A%s'%i].value","sheet['F%s'%i].value"')"
    sql = "insert into tbinfo(K_CODE,K_NAME,SHOP_NAME,SHOP_SUM,Y_TIME) values('{}','{}','{}','{}','{}')" .format(K_CODE,K_NAME,SHOP_NAME,SHOP_SUM,today)
    cursor.execute(sql)
    db.commit()

print('写入完成')

sheet = wb['Shell3']
# print(sheet['A4'].value)

for i in range(2,sheet.max_row):
    #日期
    # today = datetime.now.strftime("%Y-%m-%d")
    today = datetime.date.today()

    K_CODE = str(sheet['B%s'%i].value)
    K_NAME = str(sheet['C%s'%i].value)
    SHOP_NAME = str(sheet['A%s'%i].value)
    SHOP_SUM = str(sheet['F%s'%i].value)
    print('写入%s' % SHOP_NAME)
    # 写入内容
    # sql = "insert into shopinfo(K_CODE,K_NAME,SHOP_NAME,SHOP_SUM,Y_TIME) values('"+K_CODE+"','"+K_NAME+"','"+SHOP_NAME+"','"+SHOP_SUM+"','"+today+"')"
    # sql = "insert into shopinfo(K_CODE,K_NAME,SHOP_NAME,SHOPSUM) values('"sheet['B%s'%i].value","sheet['C%s'%i].value","sheet['A%s'%i].value","sheet['F%s'%i].value"')"
    sql = "insert into pddinfo(K_CODE,K_NAME,SHOP_NAME,SHOP_SUM,Y_TIME) values('{}','{}','{}','{}','{}')" .format(K_CODE,K_NAME,SHOP_NAME,SHOP_SUM,today)
    cursor.execute(sql)
    db.commit()

print('写入完成')


db.close()

 

你可能感兴趣的:(python 读取excel写入mysql)