python excel数据存入mysql
发布日期:2021-05-14 23:04:12 浏览次数:28 分类:精选文章

本文共 4378 字,大约阅读时间需要 14 分钟。


������������������Excel���������������������������������������������Python������������������������������������������������������Excel������������������������������������������������������������������


���������������������

  • ���������������������

    ������PyMySQL���������������������������������������������������������������������������������������������������������������������������������������������

    import pymysql
    try:
    db = pymysql.connect(
    host="127.0.0.1", # ������������������������
    user="root", # ������������������
    passwd="123456", # ���������������
    db="fresh", # ���������������
    charset="utf8" #������������������
    )
    except:
    print("could not connect to mysql server")

    ������������������������������������������������������������������������


  • Excel���������������������

  • Excel������������

    ������Pybernate���xlrd���������Excel���������������������������������������������

    def open_excel():
    try:
    book = xlrd.open_workbook(r'D:\db\area.xlsx') # ������������������������
    except:
    print("open excel file failed!" )
    try:
    sheet = book.sheet_by_name("���������������")
    return sheet
    except:
    print("locate worksheet in excel failed!")

    ���������������������������������������������Excel���������������������sheet���������������������

  • ������������������

    ������Excel���������������������������������������������������������

    def insert_deta():
    sheet = open_excel()
    cursor = db.cursor()
    row_num = sheet.nrows # ���������������
    for i in range(1, row_num): # ������������������������
    row_data = sheet.row_values(i)
    # ���������������������������
    value = (
    int(row_data[0]), # id
    int(row_data[1]), # pid
    row_data[2], # name
    int(row_data[23]) # level
    )
    # ������������������
    sql = "INSERT INTO area (id, pid, name, level) VALUES(%s, %s, %s, %s)"
    cursor.execute(sql, value)
    db.commit()
    cursor.close()
  • ���������������������

    ���������������������������������������������������������SQL���������������������������������������

    # ������������������SQL������������������
    cursor.execute(sql, (value[0], value[1], value[2], value[3]))

  • ������������������������

  • ���������������������

    ������PyMySQL������������������������������������������

    • server_address: ������������������IP������
    • database_name: ������������������������
    • charset: ���������������������������utf-8
  • Excel������������������

    ������xlrd���������Excel������������������������������������

    • ������������������������
    • ������������������������������������������������
    • ���������������������������������������������
  • ������������������

    ���������������������������������������������������������

    • ���������������������
    • ���������������������
    • execute������SQL������
    • ������������������

  • ���������������������

  • ������������������������

    ���������������������������������Python������

    pip install pymysql xlrd
  • ���������������������

    ���������������������������������������������������������������

    user = "���������username"  # ������������������������
    passwd = "���������������" # ���������������������
  • ������������������

    ������Excel���������������������������������������������������������������������������Python������������������������������������������������������


  • ������������������

    • ���������������������������������������������������Excel���������������������������
    • ������������������������������������Python���������������������������������������������������������
    • ������������������������������������������������������������������������������������

    ���������������������

  • ������������������

    ���������������������������������Excel���������������������������������������������������������������������

  • ������������������

    ���������������������������������������������������������������������

    # ������������������������������������
    print(f"���������������{i}������������{row_data}")
  • ������������������

    • ���������������������������������������������������
    • ������������������������������������������������������������
    • ������Excel���������������������������������������

  • ������������������������������������������������Excel���������������������������������������������������������������������������Excel���������������������������������������������������������������������������������������������������

    上一篇:SQL 分页查询 返回总条数
    下一篇:小程序/JS 实现计时器

    发表评论

    最新留言

    关注你微信了!
    [***.104.42.241]2025年04月07日 06时57分02秒