【问题标题】:Pandas to_sql to localhost table returns 'Engine' object has no attribute 'cursor'Pandas to_sql 到 localhost 表返回“引擎”对象没有属性“光标”
【发布时间】:2021-12-25 09:05:32
【问题描述】:

我看到很多这个问题是关于 sqlite 的,但我的问题是关于 MySQL 的。

我的整个脚本是这样的:

df = pd.read_csv("df.csv")
engine = sqlalchemy.create_engine('mysql+mysqlconnector://{0}:{1}@{2}/{3}'.
                                               format(config.user, config.passwd, 
                                                      config.host, config.db))

df.to_sql('SQL_table', con=engine, if_exists='append', index=False)

然后它返回错误:

 'Engine' object has no attribute 'cursor'

我用谷歌搜索,并遵循了一些解决方案,其中之一是:

df = pd.read_csv("df.csv")
engine = sqlalchemy.create_engine('mysql+mysqlconnector://{0}:{1}@{2}/{3}'.
                                                   format(config.user, config.passwd, 
                                                          config.host, config.db))

connection = engine.raw_connection()    
df.to_sql('SQL_table', con=connection, if_exists='append', index=False)

然后错误变为:

DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': Not all parameters were used in the SQL statement

我使用的是 MySQL,而不是 sqlite,我不明白为什么它会返回此错误。

所以基本上,我认为解决方案不起作用,谁能告诉我如何解决这个问题,我的 SQLalchemy 是 1.4.27

【问题讨论】:

    标签: mysql pandas dataframe sqlalchemy pandas-to-sql


    【解决方案1】:

    我已经解决了这个问题,我重置了我的 Mac,然后回到我的 VS Code,再次启动笔记本,问题就消失了。

    但在那之前,我也尝试过使用命令

    reset
    

    这不能解决问题。它必须是机器硬重置。

    【讨论】:

      猜你喜欢
      • 2016-11-14
      • 2021-01-09
      • 2019-03-17
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      • 2021-02-06
      • 2018-02-08
      • 1970-01-01
      相关资源
      最近更新 更多