r"""Produce a "bound expression". The return value is an instance of :class:`.BindParameter`; this is a :class:`_expression.ColumnElement` subclass which represents a so-called "placeholder" value in a SQL expression, the value of which is supplied at the point at which the stat
(
key: Optional[str],
value: Any = _NoArg.NO_ARG,
type_: Optional[_TypeEngineArgument[_T]] = None,
unique: bool = False,
required: Union[bool, Literal[_NoArg.NO_ARG]] = _NoArg.NO_ARG,
quote: Optional[bool] = None,
callable_: Optional[Callable[[], Any]] = None,
expanding: bool = False,
isoutparam: bool = False,
literal_execute: bool = False,
)
| 475 | |
| 476 | |
| 477 | def bindparam( |
| 478 | key: Optional[str], |
| 479 | value: Any = _NoArg.NO_ARG, |
| 480 | type_: Optional[_TypeEngineArgument[_T]] = None, |
| 481 | unique: bool = False, |
| 482 | required: Union[bool, Literal[_NoArg.NO_ARG]] = _NoArg.NO_ARG, |
| 483 | quote: Optional[bool] = None, |
| 484 | callable_: Optional[Callable[[], Any]] = None, |
| 485 | expanding: bool = False, |
| 486 | isoutparam: bool = False, |
| 487 | literal_execute: bool = False, |
| 488 | ) -> BindParameter[_T]: |
| 489 | r"""Produce a "bound expression". |
| 490 | |
| 491 | The return value is an instance of :class:`.BindParameter`; this |
| 492 | is a :class:`_expression.ColumnElement` |
| 493 | subclass which represents a so-called |
| 494 | "placeholder" value in a SQL expression, the value of which is |
| 495 | supplied at the point at which the statement in executed against a |
| 496 | database connection. |
| 497 | |
| 498 | In SQLAlchemy, the :func:`.bindparam` construct has |
| 499 | the ability to carry along the actual value that will be ultimately |
| 500 | used at expression time. In this way, it serves not just as |
| 501 | a "placeholder" for eventual population, but also as a means of |
| 502 | representing so-called "unsafe" values which should not be rendered |
| 503 | directly in a SQL statement, but rather should be passed along |
| 504 | to the :term:`DBAPI` as values which need to be correctly escaped |
| 505 | and potentially handled for type-safety. |
| 506 | |
| 507 | When using :func:`.bindparam` explicitly, the use case is typically |
| 508 | one of traditional deferment of parameters; the :func:`.bindparam` |
| 509 | construct accepts a name which can then be referred to at execution |
| 510 | time:: |
| 511 | |
| 512 | from sqlalchemy import bindparam |
| 513 | |
| 514 | stmt = select(users_table).where( |
| 515 | users_table.c.name == bindparam("username") |
| 516 | ) |
| 517 | |
| 518 | The above statement, when rendered, will produce SQL similar to: |
| 519 | |
| 520 | .. sourcecode:: sql |
| 521 | |
| 522 | SELECT id, name FROM user WHERE name = :username |
| 523 | |
| 524 | In order to populate the value of ``:username`` above, the value |
| 525 | would typically be applied at execution time to a method |
| 526 | like :meth:`_engine.Connection.execute`:: |
| 527 | |
| 528 | result = connection.execute(stmt, {"username": "wendy"}) |
| 529 | |
| 530 | Explicit use of :func:`.bindparam` is also common when producing |
| 531 | UPDATE or DELETE statements that are to be invoked multiple times, |
| 532 | where the WHERE criterion of the statement is to change on each |
| 533 | invocation, such as:: |
| 534 |