我正在try 解决我认为很简单的问题:将Pandas中的数据框导出到MySQL数据库中.
有一个用于在PANAS中整理数据以保存CSV格式的刮取器
**title, summary, url** #header
abc, summary, some_url
但我想将数据帧直接发送到具有相同三列格式的MySQL数据库.
到目前为止我的代码是:
import mysql.connector
# Connect to the database
conn = mysql.connector.connect(user='root', password='somepassword', host='localhost', port='3306', database='db')
# Write the DataFrame to the database
df.to_sql(name='table_name', con=conn, if_exists='replace', index=False)
# Close the connection
conn.close()
但这将返回一条错误消息:
pandas.errors.DatabaseError: Execution failed on sql 'SELECT name FROM sqlite_master WHERE type='table' AND name=?;': Not all parameters were used in the SQL statement
我该怎么解决这个问题?
最新情况:
我读到我可能不得不使用SqlalChemy,但如果可能的话,我真的想坚持使用Pandas 的解决方案.