pandas 数据框到 mariadb 数据库 to_sql 和 sqlalchemy
pandas dataframe to mariadb database with to_sql and sqlalchemy
我想使用 pandas 函数 to_sql 将数据帧写入 MariaDB 数据库。我在 PyCharm 中的 Python 代码如下所示:
import pandas as pd
import mysql.connector
from sqlalchemy import create_engine
myd = pd.read_csv('/[path]/[filename].csv')
engine = create_engine('mysql+mysqlconnector://[user]:[pw]@127.0.0.1/[dbname]')
myd.to_sql(name='[tablename]', con=engine, if_exists='replace', index=False)
在控制台中执行最后一行时出现以下错误:
Error on sql SELECT name FROM sqlite_master WHERE type='table' AND name='[tablename]';
Traceback (most recent call last):
File "/usr/lib/python3.4/code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "/usr/lib/python3/dist-packages/pandas/core/frame.py", line 1261, in to_sql
self, name, con, flavor=flavor, if_exists=if_exists, **kwargs)
File "/usr/lib/python3/dist-packages/pandas/io/sql.py", line 207, in write_frame
exists = table_exists(name, con, flavor)
File "/usr/lib/python3/dist-packages/pandas/io/sql.py", line 275, in table_exists
return len(tquery(query, con)) > 0
File "/usr/lib/python3/dist-packages/pandas/io/sql.py", line 90, in tquery
cur = execute(sql, con, cur=cur)
File "/usr/lib/python3/dist-packages/pandas/io/sql.py", line 44, in execute
cur = con.cursor()
AttributeError: 'Engine' object has no attribute 'cursor'
有人曾经有过同样的错误。然而,在有人解决问题之前,它已经消失了。你知道哪里不对吗?
仅从 pandas 0.14.0
开始支持传递 sqlalchemy 引擎
要将 to_sql
与较早的 pandas 版本一起使用,您需要将原始连接 (engine.raw_connection()
) 和 flavor='mysql'
传递给 to_sql
:
myd.to_sql(name='[tablename]', con=engine.raw_connection(), flavor='mysql', if_exists='replace', index=False)
但是,我建议升级您的 pandas 版本(传递原始连接已被弃用,并且在较新的 pandas 版本中将不再受支持,那么将仅支持 sqlalchemy engines/connections )
我想使用 pandas 函数 to_sql 将数据帧写入 MariaDB 数据库。我在 PyCharm 中的 Python 代码如下所示:
import pandas as pd
import mysql.connector
from sqlalchemy import create_engine
myd = pd.read_csv('/[path]/[filename].csv')
engine = create_engine('mysql+mysqlconnector://[user]:[pw]@127.0.0.1/[dbname]')
myd.to_sql(name='[tablename]', con=engine, if_exists='replace', index=False)
在控制台中执行最后一行时出现以下错误:
Error on sql SELECT name FROM sqlite_master WHERE type='table' AND name='[tablename]';
Traceback (most recent call last):
File "/usr/lib/python3.4/code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "/usr/lib/python3/dist-packages/pandas/core/frame.py", line 1261, in to_sql
self, name, con, flavor=flavor, if_exists=if_exists, **kwargs)
File "/usr/lib/python3/dist-packages/pandas/io/sql.py", line 207, in write_frame
exists = table_exists(name, con, flavor)
File "/usr/lib/python3/dist-packages/pandas/io/sql.py", line 275, in table_exists
return len(tquery(query, con)) > 0
File "/usr/lib/python3/dist-packages/pandas/io/sql.py", line 90, in tquery
cur = execute(sql, con, cur=cur)
File "/usr/lib/python3/dist-packages/pandas/io/sql.py", line 44, in execute
cur = con.cursor()
AttributeError: 'Engine' object has no attribute 'cursor'
仅从 pandas 0.14.0
开始支持传递 sqlalchemy 引擎要将 to_sql
与较早的 pandas 版本一起使用,您需要将原始连接 (engine.raw_connection()
) 和 flavor='mysql'
传递给 to_sql
:
myd.to_sql(name='[tablename]', con=engine.raw_connection(), flavor='mysql', if_exists='replace', index=False)
但是,我建议升级您的 pandas 版本(传递原始连接已被弃用,并且在较新的 pandas 版本中将不再受支持,那么将仅支持 sqlalchemy engines/connections )