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

相关推荐
有泽改之_几秒前
Garmin FIT协议与FIT Activity文件类型
网络·python
..Move...几秒前
企业级 K8s 中间件部署(Mysql主从)
mysql·中间件·kubernetes
a***9768几秒前
Python大数据可视化:基于大数据技术的共享单车数据分析与辅助管理系统_flask+hadoop+spider
大数据·python·信息可视化
h***06651 分钟前
docker 安装 mysql
mysql·adb·docker
love530love2 分钟前
【笔记】解决 Stable Diffusion WebUI 启动 “找不到llama_cpp模块”
运维·windows·笔记·python·stable diffusion·github·llama
h***67375 分钟前
Flask:后端框架使用
后端·python·flask
野生技术架构师6 分钟前
MySQL同步ES的 5 种方案
数据库·mysql·elasticsearch
L***86538 分钟前
flask后端开发(8):Flask连接MySQL数据库+ORM增删改查
数据库·mysql·flask
MediaTea9 分钟前
Python 库手册:gc 垃圾回收
java·开发语言·jvm·python·算法
红蒲公英10 分钟前
( 教学 )Agent 构建 Prompt(提示词)2. CommaSeparatedListOutputParser
人工智能·python·langchain·prompt·langgraph