首页 > 解决方案 > 带有 if_exists="append" 的 df.to_sql 失败并出现 TypeError: Invalid Argument sent to create_engine

问题描述

我目前正在使用 0.24.2 的 Pandas、11.2 的 Postgresql 和 1.3.2 的 sqlalchemy 尝试使用if_exists="append". 代码运行时返回以下错误:

TypeError: Invalid argument(s) 'if_exists' sent to create_engine(), using
configuration PGDialect_psycopg2/QueuePool/Engine.  Please check that the
keyword arguments are appropriate for this combination of components.

代码很简单,看起来与文档中的示例完全相同:

def write_to_db(df):
   engine = create_engine(
         "postgresql://esammons@localhost:5432/testdb", if_exists='append'
        )
   df.to_sql("ctcl", engine)

该代码在没有该if_exists选项的情况下执行。

标签: pythonpandassqlalchemy

解决方案


尝试:

def write_to_db(df):
   engine = create_engine(
         "postgresql://esammons@localhost:5432/testdb"
        )
   df.to_sql("ctcl", engine, if_exists='append')

推荐阅读