Pandas to_sql 到 sqlite 返回 'Engine' 对象没有属性 'cursor'

标签 pandas sqlalchemy

我在 pandas 中有一个 working_df 我想输出到 sqlite 数据库。

from sqlalchemy import create_engine

sql_engine = create_engine('sqlite:///test.db', echo=False)
working_df.to_sql('data', sql_engine,index=False, if_exists='append')

返回:AttributeError: 'Engine' object has no attribute 'cursor'
有什么想法吗?

Pandas 版'0.18.1'
编辑:添加了完整的跟踪
AttributeError                            Traceback (most recent call last)
<ipython-input-41-4f64fc939721> in <module>()
----> 1 working_df.to_sql('data', engine, index=False, if_exists='append')

/Users/tom/anaconda/envs/data_science/lib/python3.5/site-packages/pandas/core/generic.py in to_sql(self, name, con, flavor, schema, if_exists, index, index_label, chunksize, dtype)
   1163         sql.to_sql(self, name, con, flavor=flavor, schema=schema,
   1164                    if_exists=if_exists, index=index, index_label=index_label,
-> 1165                    chunksize=chunksize, dtype=dtype)
   1166 
   1167     def to_pickle(self, path):

/Users/tom/anaconda/envs/data_science/lib/python3.5/site-packages/pandas/io/sql.py in to_sql(frame, name, con, flavor, schema, if_exists, index, index_label, chunksize, dtype)
    569     pandas_sql.to_sql(frame, name, if_exists=if_exists, index=index,
    570                       index_label=index_label, schema=schema,
--> 571                       chunksize=chunksize, dtype=dtype)
    572 
    573 

/Users/tom/anaconda/envs/data_science/lib/python3.5/site-packages/pandas/io/sql.py in to_sql(self, frame, name, if_exists, index, index_label, schema, chunksize, dtype)
   1659                             if_exists=if_exists, index_label=index_label,
   1660                             dtype=dtype)
-> 1661         table.create()
   1662         table.insert(chunksize)
   1663 

/Users/tom/anaconda/envs/data_science/lib/python3.5/site-packages/pandas/io/sql.py in create(self)
    688 
    689     def create(self):
--> 690         if self.exists():
    691             if self.if_exists == 'fail':
    692                 raise ValueError("Table '%s' already exists." % self.name)

/Users/tom/anaconda/envs/data_science/lib/python3.5/site-packages/pandas/io/sql.py in exists(self)
    676 
    677     def exists(self):
--> 678         return self.pd_sql.has_table(self.name, self.schema)
    679 
    680     def sql_schema(self):

/Users/tom/anaconda/envs/data_science/lib/python3.5/site-packages/pandas/io/sql.py in has_table(self, name, schema)
   1674         query = flavor_map.get(self.flavor)
   1675 
-> 1676         return len(self.execute(query, [name, ]).fetchall()) > 0
   1677 
   1678     def get_table(self, table_name, schema=None):

/Users/tom/anaconda/envs/data_science/lib/python3.5/site-packages/pandas/io/sql.py in execute(self, *args, **kwargs)
   1557             cur = self.con
   1558         else:
-> 1559             cur = self.con.cursor()
   1560         try:
   1561             if kwargs:

AttributeError: 'Engine' object has no attribute 'cursor'

最佳答案

添加 raw_connection() 对我有用

from sqlalchemy import create_engine

sql_engine = create_engine('sqlite:///test.db', echo=False)
connection = sql_engine.raw_connection()
working_df.to_sql('data', connection, index=False, if_exists='append')
我有 conda install sqlalchemy在我的笔记本 session 期间,虽然它可以访问,但由于我已经启动了 pandas,所以看起来好像没有 sqlalchemy。重新启动 session 允许我删除 raw_connection()。
from sqlalchemy import create_engine

sql_engine = create_engine('sqlite:///test.db', echo=False)
connection = sql_engine
working_df.to_sql('data', connection, index=False, if_exists='append')

关于 Pandas to_sql 到 sqlite 返回 'Engine' 对象没有属性 'cursor',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38332787/

相关文章:

python - 根据条件从数据帧创建 python 列表

sql - 查询和选择SQLAlchemy中的特定列

python - 使用多索引 reshape 数据框并相应地填充数据

python - 将带有包装标题的 excel 文件读入 pandas

python - for循环并在Python中添加额外的列groupby pandas dataframe

python - SQLAlchemy:测试关系是否存在而不实际加载它

python - Pylons + AuthKit 中的用户身份验证

python - 过滤列表中的每个项目以检查它是否在 ID 列表中

mysql - Sqlalchemy & Pylons : Adding new tables to a previously defined database?

Python - Pandas,重新采样数据集以具有平衡的类