如何将PANDAS DataFrame写入SQL Server表?

发布于 2025-02-14 00:27:46 字数 558 浏览 0 评论 0原文

我有一个熊猫数据框架,我想将其写入SQL数据库,

dfmodwh
date subkey amount age 
09/12 0012  12.8   18
09/13 0009  15.0   20

SQL仓库中有一个现有表,上面有相同的列名称。该表被称为DIM.H2OULTS,

我尝试过

import pyodbc
conn = pyodbc.connect('dsn=azure_warehouse_dev;'
                      'Trusted_Connection=yes;')


from sqlalchemy import create_engine, MetaData, Table, select
dfmodwh.to_sql(name='dim.h2oresults',con=conn, index=False, if_exists='append')

,但这只是给我执行错误。有没有办法通过pyodbc而不是sqlalchemy写入表,以便如果DFModWH中每天都有新数据,那就不断添加而不是添加写作?

I have a pandas dataframe which i want to write over to sql database

dfmodwh
date subkey amount age 
09/12 0012  12.8   18
09/13 0009  15.0   20

there is an existing table in sql warehouse with the same column names. The table is called dim.h2oresults

I tried

import pyodbc
conn = pyodbc.connect('dsn=azure_warehouse_dev;'
                      'Trusted_Connection=yes;')


from sqlalchemy import create_engine, MetaData, Table, select
dfmodwh.to_sql(name='dim.h2oresults',con=conn, index=False, if_exists='append')

But this just gives me an execution error. Is there a way to write to the table through pyodbc instead of sqlalchemy such that if there is a new data everyday in dfmodwh it just keeps appending and not over writing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清君侧 2025-02-21 00:27:46

我相信您将要使用to_sql()

df.to_sql('<Table_Name', con=<Your_Connection>, if_exists='append') 

I believe you are going to want to use to_sql()

df.to_sql('<Table_Name', con=<Your_Connection>, if_exists='append') 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文