AttributeError: 'str' object has no attribute '_execute_on_connection'
Question:
I have a problem with following code:
from pandasql import sqldf
import pandas as pd
df = pd.DataFrame({'column1': [1, 2, 3], 'column2': [4, 5, 6]})
query = "SELECT * FROM df WHERE column1 > 1"
new_dataframe = sqldf(query)
print(new_dataframe)
When I submit, I have this error:
Traceback (most recent call last):
File ~AppDataLocalProgramsSpyderPythonlibsite-packagessqlalchemyenginebase.py:1410 in execute
meth = statement._execute_on_connection
AttributeError: 'str' object has no attribute '_execute_on_connection'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File ~AppDataLocalProgramsSpyderpkgsspyder_kernelspy3compat.py:356 in compat_exec
exec(code, globals, locals)
File c:usersyv663dzdownloadsuntitled1.py:18
new_dataframe = sqldf(query)
File ~AppDataLocalProgramsSpyderPythonlibsite-packagespandasqlsqldf.py:156 in sqldf
return PandaSQL(db_uri)(query, env)
File ~AppDataLocalProgramsSpyderPythonlibsite-packagespandasqlsqldf.py:61 in __call__
result = read_sql(query, conn)
File ~AppDataLocalProgramsSpyderPythonlibsite-packagespandasiosql.py:592 in read_sql
return pandas_sql.read_query(
File ~AppDataLocalProgramsSpyderPythonlibsite-packagespandasiosql.py:1557 in read_query
result = self.execute(*args)
File ~AppDataLocalProgramsSpyderPythonlibsite-packagespandasiosql.py:1402 in execute
return self.connectable.execution_options().execute(*args, **kwargs)
File ~AppDataLocalProgramsSpyderPythonlibsite-packagessqlalchemyenginebase.py:1412 in execute
raise exc.ObjectNotExecutableError(statement) from err
ObjectNotExecutableError: Not an executable object: 'SELECT * FROM df WHERE column1 > 1'
I installed the latest versions of pandas, pandasql and sqlalchemy and I use Spyder as IDE. Could someone help me please?
Answers:
SQLAlchemy 2.0 (released 2023-01-26) requires that raw SQL queries be wrapped by sqlalchemy.text
.
So you can do
from sqlalchemy import text
...
query = text("SELECT * FROM df WHERE column1 > 1")
Alternatively you can downgrade your SQLAlchemy installation using your Python package manager. For example, if you use pip
:
python3 -m pip install --upgrade 'sqlalchemy<2.0'
I encountered the same problem and after I fixed myquery
by wrapping text(query)
, I got typeerror: expected string or bytes-like object
.
I have a problem with following code:
from pandasql import sqldf
import pandas as pd
df = pd.DataFrame({'column1': [1, 2, 3], 'column2': [4, 5, 6]})
query = "SELECT * FROM df WHERE column1 > 1"
new_dataframe = sqldf(query)
print(new_dataframe)
When I submit, I have this error:
Traceback (most recent call last):
File ~AppDataLocalProgramsSpyderPythonlibsite-packagessqlalchemyenginebase.py:1410 in execute
meth = statement._execute_on_connection
AttributeError: 'str' object has no attribute '_execute_on_connection'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File ~AppDataLocalProgramsSpyderpkgsspyder_kernelspy3compat.py:356 in compat_exec
exec(code, globals, locals)
File c:usersyv663dzdownloadsuntitled1.py:18
new_dataframe = sqldf(query)
File ~AppDataLocalProgramsSpyderPythonlibsite-packagespandasqlsqldf.py:156 in sqldf
return PandaSQL(db_uri)(query, env)
File ~AppDataLocalProgramsSpyderPythonlibsite-packagespandasqlsqldf.py:61 in __call__
result = read_sql(query, conn)
File ~AppDataLocalProgramsSpyderPythonlibsite-packagespandasiosql.py:592 in read_sql
return pandas_sql.read_query(
File ~AppDataLocalProgramsSpyderPythonlibsite-packagespandasiosql.py:1557 in read_query
result = self.execute(*args)
File ~AppDataLocalProgramsSpyderPythonlibsite-packagespandasiosql.py:1402 in execute
return self.connectable.execution_options().execute(*args, **kwargs)
File ~AppDataLocalProgramsSpyderPythonlibsite-packagessqlalchemyenginebase.py:1412 in execute
raise exc.ObjectNotExecutableError(statement) from err
ObjectNotExecutableError: Not an executable object: 'SELECT * FROM df WHERE column1 > 1'
I installed the latest versions of pandas, pandasql and sqlalchemy and I use Spyder as IDE. Could someone help me please?
SQLAlchemy 2.0 (released 2023-01-26) requires that raw SQL queries be wrapped by sqlalchemy.text
.
So you can do
from sqlalchemy import text
...
query = text("SELECT * FROM df WHERE column1 > 1")
Alternatively you can downgrade your SQLAlchemy installation using your Python package manager. For example, if you use pip
:
python3 -m pip install --upgrade 'sqlalchemy<2.0'
I encountered the same problem and after I fixed myquery
by wrapping text(query)
, I got typeerror: expected string or bytes-like object
.