[Solved]-Pandas to_sql to sqlite returns 'Engine' object has no attribute 'cursor'


The error "Engine' object has no attribute 'cursor'" while using Pandas to_sql to write data to a SQLite database, it indicates an issue with the SQLAlchemy library's compatibility with the SQLite engine. This problem may be arises due to an outdated or incompatible version of SQLAlchemy.

To resolve this error, you should ensure that you have the latest version of SQLAlchemy installed. You can do this by upgrading your SQLAlchemy library using pip:

pip install --upgrade sqlalchemy

After upgrading SQLAlchemy, you can try running your code again. 

Here's simple an example of how you can use Pandas to write a DataFrame to a SQLite database:

import pandas as pd
from sqlalchemy import create_engine

# Sample DataFrame
data = {'Name': ['John', 'Emma', 'Ryan'],
        'Age': [28, 35, 32]}
df = pd.DataFrame(data)

# SQLite connection
engine = create_engine('sqlite:///test.db')

# Write DataFrame to SQLite
df.to_sql('people', con=engine, index=False, if_exists='replace')

Ensure that you replace 'test.db' with the path to your SQLite database file. The if_exists='replace' parameter specifies what to do if the table already exists. You can change it according to your application requirements.

By following these steps, you should be able to resolve the "Engine' object has no attribute 'cursor'" error when using Pandas to write to a SQLite database.

During our session in the notebook, we encountered an issue where using Pandas to write to a SQLite database resulted in the error "Engine' object has no attribute 'cursor'". After some investigation, we found a solution that worked for us.

First, we imported the necessary libraries:

from sqlalchemy import create_engine

Then, we created a SQLAlchemy engine for SQLite:

sql_engine = create_engine('sqlite:///sampleDemo.db', echo=False)

Next, we established a raw connection to the SQLite database:

connection = sql_engine.raw_connection()

With the connection established, we were able to successfully write our DataFrame to the SQLite database using to_sql():

working_df.to_sql('data', connection, index=False, if_exists='append')

This approach resolved the error for us and allowed us to append data to the SQLite database without encountering the "Engine' object has no attribute 'cursor'" issue.

It's worth noting that the root cause of the error was likely due to some inconsistency in the environment. Since we had already installed SQLAlchemy via Conda, it should have been accessible. However, due to the way our notebook session was set up, it appeared as if SQLAlchemy wasn't recognized. 

Restarting the session helped resolve this inconsistency, allowing us to use the engine directly without the need for raw_connection().

In summary, while using raw_connection() can be a workaround in certain situations, it's advisable to ensure that your environment is properly configured and consistent to avoid such issues in the future.

After updating Pandas from version 2.1.4 to 2.2.0, we encountered an issue where our project, which previously worked flawlessly, started throwing an AttributeError. This issue originate from a modification in how a SQLAlchemy Engine object operates when passed to the con argument for functions like pd.read_sql and pd.to_sql.

Fortunately, we found a solution to this problem:

Instead of using the Engine object directly, we utilized the connection attribute of a SQLAlchemy Connection object.

Here's an example demonstrating the solution:

from sqlalchemy import create_engine
import pandas as pd

# Engine for MS SQL Server using pymssql
conn_url = f"mssql+pymssql://{username}:{password}@{host}/{database}"
engine = create_engine(conn_url)

# Example query
query = 'select id from users'

# Works with pandas<2.2.0
df = pd.read_sql(
    sql=query,
    con=engine,
)

# Works with pandas==2.2.0
with engine.connect() as conn:
    df = pd.read_sql(
        sql=query,
        con=conn.connection
    )

This solution ensures compatibility with Pandas version 2.2.0 and resolves the AttributeError issue caused by changes in the way Engine objects are handled.

By using the connection attribute of a SQLAlchemy Connection object, we can maintain compatibility with both older and newer versions of Pandas, ensuring smooth operation of our project.

The AttributeError: 'Engine' object has no attribute 'cursor' while using Pandas' read_sql() function, it suggests that there's an issue with how the Engine object is being handled.

To resolve this error, you can follow these steps:

  1. Ensure you have the latest version of Pandas installed. You can upgrade Pandas using pip:
pip install --upgrade pandas
  1. Instead of passing the Engine object directly to read_sql(), utilize the connect() method of the engine to establish a connection and then pass the resulting connection object to read_sql().

Here's sample code example demonstrating the solution:

from sqlalchemy import create_engine
import pandas as pd

# Create an SQLAlchemy engine
engine = create_engine('sqlite:///example.db')

# Establish a connection using connect() method
connection = engine.connect()

# Example SQL query
query = "SELECT * FROM your_table"

# Use read_sql() with the connection object
df = pd.read_sql(query, connection)

# Close the connection
connection.close()

By following these steps and using the connection object instead of the Engine object directly, you should be able to avoid the AttributeError and successfully read data from your SQL database using Pandas.

After encountering the issue with the AttributeError while using read_sql() and other Pandas SQL functions, we tried a few troubleshooting steps and found a solution that worked for us.

  • Firstly, we saved our workbook, then restarted the PC, and finally reloaded the workbook. It appeared that something was cached, and by performing these steps, we were able to clear the cache and resolve the problem.
  • Subsequently, we found that we could use read_sql() and other Pandas SQL functions by passing the 'engine' reference directly.

Saving the workbook, restarting the PC, and reloading helped clear any cached data or configurations that might have been causing the AttributeError. Additionally, passing the 'engine' reference directly to read_sql() is a standard practice and ensures smooth interaction with the SQL database using Pandas.