我正在使用以下代码通过 SQL alchemy 连接到 MySQL 数据库。
from sqlalchemy import create_engine
import pandas as pd
query = "SELECT * FROM hello"
engine = create_engine("mysql+pymysql://root:new_pass@localhost:3306/toronto_analytics")
engine = engine.raw_connection()
df = pd.DataFrame({"bob":"hello", "joe":14}, index=[0])
df.to_sql('new_table', engine)
连接到数据库后,我尝试使用数据框中的值创建一个新表。
我连接到数据库正常,但随后df.to_sql抛出以下错误:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/io/sql.py in execute(self, *args, **kwargs)
1377 else:
-> 1378 cur.execute(*args)
1379 return cur
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pymysql/cursors.py in execute(self, query, args)
167
--> 168 query = self.mogrify(query, args)
169
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pymysql/cursors.py in mogrify(self, query, args)
146 if args is not None:
--> 147 query = query % self._escape_args(args, conn)
148
TypeError: not all arguments converted during string formatting
During handling of the above exception, another exception occurred:
DatabaseError Traceback (most recent call last)
<ipython-input-100-03413ab65608> in <module>
8
9 df = pd.DataFrame({"bob":"hedawo", "joe":14}, index=[0])
---> 10 df.to_sql('newawa', engine)
任何有关此错误的帮助将不胜感激。谢谢
烙印99
相关分类