Skip to content
Advertisement

SQLAlchemy returns script parameters instead of script results

I’m trying to use SQLAlchemy 1.4 to query an Oracle database:

JavaScript

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.

JavaScript

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.

Advertisement

Answer

The behaviour described in the question is consistent with trying to set column names in the query using parameter binding. For example given this initial setup:

JavaScript

Then executing this code:

JavaScript

results in this output:

JavaScript

The problem is that parameter binding is designed for values, not identifiers like column or table names. To use dynamic identifiers you need to either construct the query string manually, or consider using SQLAlchemy’s reflection capabilities to build the query using objects:

JavaScript

outputs:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement