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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38332787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:33:43  来源:igfitidea点击:

Pandas to_sql to sqlite returns 'Engine' object has no attribute 'cursor'

pandassqlalchemy

提问by nahata5

I have a working_df in pandas I'd like to output to sqlite database.

我在 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')

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

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

Any thoughts?

有什么想法吗?

Pandas version '0.18.1'

Pandas版 '0.18.1'

Edit: Added full trace

编辑:添加了完整的跟踪

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'

回答by nahata5

adding in a raw_connection() worked for me

添加一个 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')

I had conda install sqlalchemyduring my notebook session, so while it was accessible, since I already initiated pandas, it appeared as if there was no sqlalchemy. Restarted the session allowed me to remove the raw_connection().

conda install sqlalchemy在我的笔记本会话期间有过,所以虽然它可以访问,但由于我已经启动了 Pandas,它看起来好像没有 sqlalchemy。重新启动会话允许我删除 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')

回答by Naveen Bhupathi

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')

Instead of above code, can you try to use connect method of the engine object instead of engine object itself as below.,

除了上面的代码,你可以尝试使用引擎对象的连接方法而不是引擎对象本身,如下所示。

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

Try above steps and let me know if you still facing the issue.

尝试上述步骤,如果您仍然遇到问题,请告诉我。

回答by Mary Hill

This is an old question but I had the same error this week and it turned out that there was a problem with MySQLdb on my Mac. As a test I tried to ‘import MySQLdb as mysql' and discovered an ImportError: “Library not loaded: libmysqlclient.18.dylib”

这是一个老问题,但本周我遇到了同样的错误,结果我的 Mac 上的 MySQLdb 存在问题。作为测试,我尝试“将 MySQLdb 导入为 mysql”并发现了一个导入错误:“库未加载:libmysqlclient.18.dylib”

The solution in my case was to add a symbolic link to the missing library as suggested in Python mysqldb: Library not loaded: libmysqlclient.18.dylib

我的解决方案是按照Python mysqldb: Library not loaded: libmysqlclient.18.dylib 中的建议添加指向缺失库的符号链接

回答by Raihan Shafique

The following code was giving me the same error while I was working to push data to MS Sql Server:

在我将数据推送到 MS Sql Server 时,以下代码给了我同样的错误:

engine_string = 'mssql+pyodbc://'+username_dsdb+':'+password_dsdb+'@'+server_dsdb+':1433/'+database_dsdb+'?driver=ODBC Driver 13 for SQL Server'
engine = sqlalchemy.create_engine(engine_string,deprecate_large_types=True)

conn_dsdb = engine.connect()

df_year_week_contract_wise.to_sql("KPIEngine_FACT_Historical", conn_dsdb, index=False, if_exists='append')

conn_dsdb.close()

But, then I restarted the session and it worked fine.

但是,然后我重新启动了会话并且它运行良好。