当前位置:网站首页>Pandas to_ SQL function pit avoidance guide "with correct code to run"
Pandas to_ SQL function pit avoidance guide "with correct code to run"
2022-04-23 05:15:00 【zzzzls~】
In use today Pandas
Of to_sql
When the method is used , There's a lot of problems , After a search , There are few reliable answers , All kinds of misguided , I wish to record
# My environment :
Mysql: 8.0.25
Python: 3.8.11
pandas: 1.3.4
sqlalchemy: 1.4.32
pymysql: 1.0.2
Let's look at it first to_sql Method parameters
DataFrame.to_sql(name,
con,
schema=None,
if_exists='fail',
index=True,
index_label=None,
chunksize=None,
dtype=None,
method=None)
There are four parameters that really need attention :
name
: The table name corresponding to the databasecon
: Database engineif_exists
: If the table exists , How to choose , The default isfail
- If database table There is , Need to be set as
append
- If database table There is , Need to be set as
index
: take DataFrame The index is written as a column , DefaultTrue
- If database table No corresponding field , Need to be set as
False
- If database table No corresponding field , Need to be set as
adopt Pandas It can be seen from the documents , Parameters con
Support only sqlalchemy.engine
perhaps sqlite3.Connection
con:sqlalchemy.engine.(Engine or Connection) or sqlite3.Connection
Using SQLAlchemy makes it possible to use any DB supported by that library. Legacy support is provided > for sqlite3.Connection objects. The user is responsible for engine disposal and connection closure for the > SQLAlchemy connectable See here.
Premise : Guarantee Dataframe Data type of each column And Mysql The data types of each field are consistent
therefore , The correct code is as follows :
import pymysql
from sqlalchemy import create_engine
pymysql.install_as_MySQLdb()
engine = create_engine('mysql+mysqldb://user:passwd@localhost/db?charset=utf8')
df.to_sql(name='user', conengine)
perhaps
import pymysql
from sqlalchemy import create_engine
engine = create_engine('mysql+pymysql://user:passwd@localhost/db?charset=utf8')
df.to_sql(name='user', conengine)
版权声明
本文为[zzzzls~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220547251985.html
边栏推荐
- scp命令详解
- 多线程基本概念(并发与并行、线程与进程)和入门案例
- 学习笔记:Unity CustomSRP-12-HDR
- DIY is an excel version of subnet calculator
- How can continuous integration (CI) / continuous delivery (CD) revolutionize automated testing
- Independent station operation | Facebook marketing artifact - chat robot manychat
- Various ways of writing timed tasks of small programs
- Summary of MySQL knowledge points
- Docker installation and mysql5 7 installation
- [2022 ICLR] Pyraformer: Low-Complexity Pyramidal Attention for Long-Range 时空序列建模和预测
猜你喜欢
Cross border e-commerce | Facebook and instagram: which social media is more suitable for you?
MySQL foreign key constraint
Basic concepts of multithreading (concurrency and parallelism, threads and processes) and entry cases
Minimum spanning tree -- unblocked project hdu1863
Good simple recursive problem, string recursive training
Thoughts on a small program
《2021年IT行业项目管理调查报告》重磅发布!
了解 DevOps,必读这十本书!
跨境电商 | Facebook 和 Instagram:哪个社交媒体更适合你?
JSP-----JSP简介
随机推荐
MySQL 慢查询
One month countdown, pgconf What are the highlights of the latest outlook of asia2021 Asian Conference?
2021 年 25 大 DevOps 工具(下)
On distributed lock
What are the redis data types
什么是指令周期,机器周期,和时钟周期?
5 minutes to understand MySQL row column conversion
Deep learning notes - fine tuning
《2021多多阅读报告》发布,95后、00后图书消费潜力攀升
DevOps生命周期,你想知道的全都在这里了!
学习笔记:Unity CustomSRP-10-Point and Spot Shadows
Basic theory of Flink
Day.js 常用方法
AQS source code reading
MySQL realizes row to column SQL
mysql5. 7. X data authorization leads to 1141
看板快速启动指南
2022/4/22
Introduction to load balancing
[database] MySQL single table query