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

相关推荐
-SGlow-18 分钟前
MySQL相关概念和易错知识点(3)(表内容的CURD、内置函数)
linux·运维·服务器·数据库·mysql
飞翔的佩奇21 分钟前
基于SpringBoot+MyBatis+MySQL+VUE实现的经方药食两用服务平台管理系统(附源码+数据库+毕业论文+部署教程+配套软件)
数据库·vue.js·spring boot·mysql·毕业设计·mybatis·经方药食两用平台
成成成成成成果2 小时前
揭秘动态测试:软件质量的实战防线
python·功能测试·测试工具·测试用例·可用性测试
瓶子xf3 小时前
Excel制作滑珠图、哑铃图
excel
天天进步20153 小时前
Python游戏开发引擎设计与实现
开发语言·python·pygame
孫治AllenSun3 小时前
【Mysql】字段隐式转换对where条件和join关联条件的影响
数据库·mysql·oracle
数据狐(DataFox)3 小时前
CTE公用表表达式的可读性与性能优化
经验分享·python·sql
蹦蹦跳跳真可爱5893 小时前
Python----MCP(MCP 简介、uv工具、创建MCP流程、MCP客户端接入Qwen、MCP客户端接入vLLM)
开发语言·人工智能·python·语言模型
No0d1es3 小时前
第13届蓝桥杯Python青少组中/高级组选拔赛(STEMA)2022年1月22日真题
python·青少年编程·蓝桥杯·选拔赛
MediaTea4 小时前
Python 库手册:getopt Unix 风格参数解析模块
服务器·开发语言·python·unix