当前位置:网站首页>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
边栏推荐
猜你喜欢
A trinomial expression that causes a null pointer
Good simple recursive problem, string recursive training
DevOps生命周期,你想知道的全都在这里了!
Making message board with PHP + MySQL
青岛敏捷之旅,来了!
7-4 is it too fat (10 points) PTA
Detailed explanation of concurrent topics
深度学习笔记 —— 数据增广
[WinUI3]編寫一個仿Explorer文件管理器
MySQL foreign key constraint
随机推荐
MySQL basics 3
洛谷P2731骑马修栅栏
[database] MySQL basic operation (basic operation ~)
On distributed lock
7-4 is it too fat (10 points) PTA
Discussion on flow restriction
项目经理值得一试的思维方式:项目成功方程式
QPushButton slot function is triggered multiple times
Day. JS common methods
The WebService interface writes and publishes calls to the WebService interface (I)
学习笔记:Unity CustomSRP-11-Post Processing---Bloom
使用 Kears 实现ResNet-34 CNN
Unity C e-learning (IV)
源码剖析Redis中如何使用跳表的
How does PostgreSQL parse URLs
Kubectl command automatic replenishment
Differences between redis and MySQL
The difference between static pipeline and dynamic pipeline
Harmonious dormitory (linear DP / interval DP)
Deep learning notes - fine tuning