python - 有什么方法可以在 python 中使用 MySQL 来持久化 Pandas 数据帧中的数据?
问题描述
有什么方法可以在 python 中使用 MySQL 来持久化 Pandas 数据帧中的数据。
解决方案
是的,你可以使用 sqlalchemy,
import pandas as pd
import mysql.connector
from sqlalchemy import create_engine
import pymysql
engine = create_engine('mysql+mysqlconnector://[user]:[pass]@[host]:[port]/[schema]',
echo=False)
df.to_sql(name='sample_table2', con=engine, if_exists = 'append', index=False)
带有 pymysql 连接器
# Connect to the database
connection = pymysql.connect(host='localhost', user='root', password='root', db='test')
cursor = connection.cursor()
# connection is not autocommit by default. So we must commit to save our changes.
connection.commit()
engine = create_engine(f"mysql+pymysql://root:root@localhost/test"
.format(user="root",
pw="root",
db="test"))
df.to_sql(name='sample_table2', con=engine, if_exists = 'append', index=False)
推荐阅读
- python - 无法通过 Python 设置 JIRA 字段受理人
- ruby-on-rails - 我的 ajax 表单提交后,如何让我的引导模式保持原位而不消失?
- linux-kernel - 从 tasklet 使用 i2c-device 操作
- c++ - 用不同的分隔符分割字符串
- gpu - NVRM:尽管客户端和内核版本相同,但 API 不匹配
- c# - 上一行填满时如何自动转到下一行int WPF C#?
- javascript - 根据单击的按钮使用 javascript 加载文件
- java - 在 Tomcat 服务器上从 Ionos 安装 SSL 证书,“未找到 Android SSL 连接的信任锚”
- html - 如何制作带有全角 visjs 的 flexbox 布局(2 行 - (2 列和 1 列))?
- r - 我可以将 shinytest 与从 Rmd 模板生成 PDF 的 Shiny 应用程序一起使用吗?