5

I try to execute essentially some sql like this (which gets builded in a complicated way):

sql = "SELECT COUNT(*) FROM entities WHERE (unit = '%')"

with this Python code:

engine.execute(sql)

What happens then is I get a

TypeError: 'dict' object does not support indexing

Why is this the case?

2
  • What database is backing your app? Commented Sep 11, 2012 at 10:07
  • If my answer below doesn't help, could you please include the full traceback to the exception please? Commented Sep 11, 2012 at 10:48

1 Answer 1

10

Not 100% sure, but I think SQLAlchemy is trying to intepret the % character as a SQL parameter. I'd try doubling the % character to work around this:

sql = "SELECT COUNT(*) FROM entities WHERE (unit = '%%')"

This depends on what database you are using; different database adapters use different parameter styles. The psycopg2 module, used for PostgreSQL database connections, for example uses the %s style and documents that %% is the correct way to insert a % literal value into your SQL statements.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this great answer. I was trying to escape with \% and getting nowhere.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.