79183216

Date: 2024-11-13 02:24:55
Score: 0.5
Natty:
Report link

@JustMe, I wound up here looking for an answer to the same question (so it wasn't just you). I suspect you've either found your answer by now or moved on, but I'll share what I found in case it might help someone else.

I found this bindparam example in the SqlAlchemy documentation for Using UPDATE and DELETE Statements, but found this syntax to work for SELECT statements as well.

First, bindparam needs to added to the sqlalchemy imports.

from sqlalchemy import bindparam

Then, it can be used in to create the placeholder in the WHERE clause.

sql = select(User).where(User.c.first_name == bindparam("username"), User.c.age == bindparam("age"))

Then, a dictionary that maps the value to the placeholder gets passed to the execute function:

user = session.execute(sql, {"username": "Tester", "age": 18})
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @JustMe
  • Low reputation (0.5):
Posted by: Alex Piotrowski