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,
)
| 2322 | |
| 2323 | @_generative |
| 2324 | def bindparams( |
| 2325 | self, |
| 2326 | *binds: BindParameter[Any], |
| 2327 | **names_to_values: Any, |
| 2328 | ) -> Self: |
| 2329 | """Establish the values and/or types of bound parameters within |
| 2330 | this :class:`_expression.TextClause` construct. |
| 2331 | |
| 2332 | Given a text construct such as:: |
| 2333 | |
| 2334 | from sqlalchemy import text |
| 2335 | |
| 2336 | stmt = text( |
| 2337 | "SELECT id, name FROM user WHERE name=:name AND timestamp=:timestamp" |
| 2338 | ) |
| 2339 | |
| 2340 | the :meth:`_expression.TextClause.bindparams` |
| 2341 | method can be used to establish |
| 2342 | the initial value of ``:name`` and ``:timestamp``, |
| 2343 | using simple keyword arguments:: |
| 2344 | |
| 2345 | stmt = stmt.bindparams( |
| 2346 | name="jack", timestamp=datetime.datetime(2012, 10, 8, 15, 12, 5) |
| 2347 | ) |
| 2348 | |
| 2349 | Where above, new :class:`.BindParameter` objects |
| 2350 | will be generated with the names ``name`` and ``timestamp``, and |
| 2351 | values of ``jack`` and ``datetime.datetime(2012, 10, 8, 15, 12, 5)``, |
| 2352 | respectively. The types will be |
| 2353 | inferred from the values given, in this case :class:`.String` and |
| 2354 | :class:`.DateTime`. |
| 2355 | |
| 2356 | When specific typing behavior is needed, the positional ``*binds`` |
| 2357 | argument can be used in which to specify :func:`.bindparam` constructs |
| 2358 | directly. These constructs must include at least the ``key`` |
| 2359 | argument, then an optional value and type:: |
| 2360 | |
| 2361 | from sqlalchemy import bindparam |
| 2362 | |
| 2363 | stmt = stmt.bindparams( |
| 2364 | bindparam("name", value="jack", type_=String), |
| 2365 | bindparam("timestamp", type_=DateTime), |
| 2366 | ) |
| 2367 | |
| 2368 | Above, we specified the type of :class:`.DateTime` for the |
| 2369 | ``timestamp`` bind, and the type of :class:`.String` for the ``name`` |
| 2370 | bind. In the case of ``name`` we also set the default value of |
| 2371 | ``"jack"``. |
| 2372 | |
| 2373 | Additional bound parameters can be supplied at statement execution |
| 2374 | time, e.g.:: |
| 2375 | |
| 2376 | result = connection.execute( |
| 2377 | stmt, timestamp=datetime.datetime(2012, 10, 8, 15, 12, 5) |
| 2378 | ) |
| 2379 | |
| 2380 | The :meth:`_expression.TextClause.bindparams` |
| 2381 | method can be called repeatedly, |