I'm trying to use SQLAlchemy 1.4 to query an Oracle database:
SELECT MAX(:created_date) FROM EXAMPLE_TABLE
This should return some kind of datetime object.
When I execute this code (roughly) using SQLAlchemy, it decides to return the value of the :created_date parameter instead.
from sqlalchemy import create_engine as SQLAlchemyEngine
from sqlalchemy import text as SQLAlchemyText
with SQLAlchemyEngine("example").connect() as sqlalchemy_connection:
with open(sql_script_path) as sql_script:
sql_query = SQLAlchemyText(sql_script.read())
parameters = {"created_date": "blah"}
result = connection.execute(sql_query, parameters)
for row in result:
print(row)
Why is the result (literally) "blah"?
EDIT:: See snakecharmerb's answer below; you can't bind column or table names with SQLAlchemy. It's a bit is hidden in the SQLAlchemy docs:
Binding Column and Table Names
Column and table names cannot be bound in SQL queries. You can concatenate text to build up a SQL statement, but make sure you use an Allow List or other means to validate the data in order to avoid SQL Injection security issues.
resultdoes always up being('blah',).