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

相关推荐
虎头金猫11 分钟前
我的远程开发革命:从环境配置噩梦到一键共享的蜕变
网络·python·网络协议·tcp/ip·beautifulsoup·负载均衡·pandas
壹号用户19 分钟前
python学习之可迭代对象&迭代器对象
python·学习
蒋星熠22 分钟前
基于深度学习的卫星图像分类(Kaggle比赛实战)
人工智能·python·深度学习·机器学习·分类·数据挖掘
虚行23 分钟前
Python学习入门
开发语言·python·学习
总有刁民想爱朕ha27 分钟前
Python自动化从入门到实战(23):Python打地鼠游戏开发
开发语言·python·游戏开发
牛奶咖啡1338 分钟前
MySQL InnoDB Cluster 高可用集群部署与应用实践(下)
数据库·mysql·innodb cluster·mysql router·mysql路由的安装部署·mysql路由的测试·mgr组复制
科学创新前沿1 小时前
机器学习催化剂设计专题学习
python·学习·机器学习·催化剂·催化剂设计
C嘎嘎嵌入式开发3 小时前
(六)机器学习之图卷积网络
人工智能·python·机器学习
DataLaboratory6 小时前
Python爬取百度地图-前端直接获取
爬虫·python·百度地图
会飞的小菠菜8 小时前
大量Excel工作簿中的图片怎么批量提取出来
excel·导出·图片·提取·批量·保存·另存为