python读取Excel到mysql

常见问题:

1.数据库密码有特殊字符

使用urllib.parse.quote_plus 编译密码

mysql_engine = create_engine((f"mysql+pymysql://root:%s@10.0.0.2:3306/mydb")%urllib.parse.quote_plus("pass@=aaaa"))

2.设置字段类型

设置特定类型,和指定日期类型

data1 = pd.read_excel("C:/work/20230818***/数据导入.xlsx",dtype={0:"int", 2:"str", 3:"str"},parse_dates=[4,5])

eg:

from sqlalchemy import create_engine

import sqlalchemy

import pandas as pd

import urllib.parse

import datetime

from io import StringIO

use this func, you need to change the cancel , which below # change the test or proc env

be sure you have encoded the tag, and truncate the old db data

if name == 'main':

data1 = pd.read_excel("C:/work/20230818***/数据导入.xlsx",dtype={0:"int", 2:"str", 3:"str"},parse_dates=[4,5])

set Create_Time

get current time

now_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

data1["Create_Time"] = now_time

set special column

data1["Oper_Sign"] = 'admin'

change the test or proc env

mysql_engine = create_engine(f"mysql+pymysql://账号名:密码@IP地址:3306/数据库名")

mysql_engine = create_engine((f"mysql+pymysql://root:%s@10.0.0.2:3306/mydb")%urllib.parse.quote_plus("pass@=aaaa"))

data1.to_sql(name='表名', con=mysql_engine, if_exists="append", index=False)

print('done')

相关推荐
蒲公英源码8 分钟前
uniapp开源ERP多仓库管理系统
mysql·elementui·uni-app·php
十重幻想15 分钟前
reshape的共享内存
python
小码过河.18 分钟前
告别 mysqldump 痛点!用 mydumper 实现 MySQL 高效备份与恢复
数据库·mysql
Juchecar24 分钟前
设计模式不是Java专属,其他语言的使用方法
java·python·设计模式
是2的10次方啊33 分钟前
MySQL索引优化实战:原则速查与踩坑案例(实战篇)
mysql
scala舔狗汪41 分钟前
双层跳动爱心❤️❤️💕💕
python
Rolei_zl1 小时前
AIGC(生成式AI)试用 40 -- 程序(Python + OCR)-2
python·aigc
可触的未来,发芽的智生1 小时前
触摸未来2025-11-09:万有力,图论革命
javascript·人工智能·python·程序人生·自然语言处理
PieroPc2 小时前
用python Streamlit 做个RapidOCR 文本识别系统
开发语言·python·ocr