Establish the values and/or types of bound parameters within this :class:`_expression.TextClause` construct. Given a text construct such as:: from sqlalchemy import text stmt = text( "SELECT id, name FROM user WHERE name=:name AND timestamp=
(
self,
*binds: BindParameter[Any],
**names_to_values: Any,
)
| 2586 | |
| 2587 | @_generative |
| 2588 | def bindparams( |
| 2589 | self, |
| 2590 | *binds: BindParameter[Any], |
| 2591 | **names_to_values: Any, |
| 2592 | ) -> Self: |
| 2593 | """Establish the values and/or types of bound parameters within |
| 2594 | this :class:`_expression.TextClause` construct. |
| 2595 | |
| 2596 | Given a text construct such as:: |
| 2597 | |
| 2598 | from sqlalchemy import text |
| 2599 | |
| 2600 | stmt = text( |
| 2601 | "SELECT id, name FROM user WHERE name=:name AND timestamp=:timestamp" |
| 2602 | ) |
| 2603 | |
| 2604 | the :meth:`_expression.TextClause.bindparams` |
| 2605 | method can be used to establish |
| 2606 | the initial value of ``:name`` and ``:timestamp``, |
| 2607 | using simple keyword arguments:: |
| 2608 | |
| 2609 | stmt = stmt.bindparams( |
| 2610 | name="jack", timestamp=datetime.datetime(2012, 10, 8, 15, 12, 5) |
| 2611 | ) |
| 2612 | |
| 2613 | Where above, new :class:`.BindParameter` objects |
| 2614 | will be generated with the names ``name`` and ``timestamp``, and |
| 2615 | values of ``jack`` and ``datetime.datetime(2012, 10, 8, 15, 12, 5)``, |
| 2616 | respectively. The types will be |
| 2617 | inferred from the values given, in this case :class:`.String` and |
| 2618 | :class:`.DateTime`. |
| 2619 | |
| 2620 | When specific typing behavior is needed, the positional ``*binds`` |
| 2621 | argument can be used in which to specify :func:`.bindparam` constructs |
| 2622 | directly. These constructs must include at least the ``key`` |
| 2623 | argument, then an optional value and type:: |
| 2624 | |
| 2625 | from sqlalchemy import bindparam |
| 2626 | |
| 2627 | stmt = stmt.bindparams( |
| 2628 | bindparam("name", value="jack", type_=String), |
| 2629 | bindparam("timestamp", type_=DateTime), |
| 2630 | ) |
| 2631 | |
| 2632 | Above, we specified the type of :class:`.DateTime` for the |
| 2633 | ``timestamp`` bind, and the type of :class:`.String` for the ``name`` |
| 2634 | bind. In the case of ``name`` we also set the default value of |
| 2635 | ``"jack"``. |
| 2636 | |
| 2637 | Additional bound parameters can be supplied at statement execution |
| 2638 | time, e.g.:: |
| 2639 | |
| 2640 | result = connection.execute( |
| 2641 | stmt, timestamp=datetime.datetime(2012, 10, 8, 15, 12, 5) |
| 2642 | ) |
| 2643 | |
| 2644 | The :meth:`_expression.TextClause.bindparams` |
| 2645 | method can be called repeatedly, |
nothing calls this directly
no test coverage detected