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')

相关推荐
阿方索10 分钟前
MySQL
数据库·mysql
Franklin15 分钟前
如何解决git HEAD detached 分离头指针问题
git·python·pycharm
彭于晏Yan23 分钟前
excel导入导出
spring boot·excel
deephub25 分钟前
Anthropic 开源 Bloom:基于 LLM 的自动化行为评估框架
人工智能·python·自动化·大语言模型·行为评估
万兴丶28 分钟前
批量转换音频格式工具
python
大学就业之家31 分钟前
智能选岗的陷阱与应对策略
python
chenshi178132 分钟前
安全合规:使用 RPA 批量发布时,如何规避平台封禁风险,实现长效稳定的投喂?
python
superman超哥44 分钟前
仓颉性能瓶颈定位方法深度解析
c语言·开发语言·c++·python·仓颉
冰冰菜的扣jio1 小时前
MySQL高性能优化合集
数据库·mysql
岁岁的O泡奶1 小时前
NSSCTF_crypto_[SWPU 2020]happy
经验分享·python·算法·密码学