2016-07-12 29 views
4

我有一個working_df熊貓我想輸出到sqlite數據庫。熊貓to_sql to 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'

有什麼想法?

熊貓版'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' 
+0

你確定選擇了正確的熊貓版嗎?你能在同一個會話中顯示'pd .__ version__'的輸出嗎?如果你有一個老熊貓版本(<0.14) – joris

+0

'pd .__ version__'返回''0.18.1'',通常會出現這個錯誤,我已經看到這是一個較老的熊貓問題,這就是爲什麼奇怪的 – nahata5

+0

正在進行舊的問題http://stackoverflow.com/questions/30631325/writing-to-mysql-database-with-pandas-using-sqlalchemy-to-sql,我做了一個'engine.raw_connection()',當應用於'to_sql',它似乎工作 – nahata5

回答

11

在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,因此,儘管它因爲我已經發起了大熊貓,所以似乎沒有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') 
+2

我正在使用Jupyter。 'pip安裝sqlalchemy'之後,我只需要重新啓動Jupyter內核以使用pandas即可使用sqlalchemy。 – jjmontes

+0

我在REPL中,並在另一個終端選項卡中安裝了SQLAlchemy,而無需重新啓動REPL。總體看來,當Pandas被加載後,SQLAlchemy需要可用。 –

+0

確實如此,@ jjmontes。在嘗試任何事情之前重新啓 – Axis

0

這是一個老問題,但本週有同樣的錯誤,結果發現我的Mac上有MySQLdb問題。作爲一個測試,我試圖「將MySQLdb導入爲mysql」,並發現一個ImportError:「Library not loaded:libmysqlclient.18.dylib」

我的例子中的解決方案是添加一個符號鏈接到缺少的庫,如Python mysqldb: Library not loaded: libmysqlclient.18.dylib

相關問題