.. automethod:: execute
:param query: The query to execute.
- :type query: `!str`, `!bytes`, `sql.SQL`, or `sql.Composed`
+ :type query: `~typing.LiteralString`, `!bytes`, `sql.SQL`, `sql.Composed`,
+ or `~string.templatelib.Template`
:param params: The parameters to pass to the query, if any.
+ Can't be specified if ``query`` is a `!Template`.
:type params: Sequence or Mapping
:param prepare: Force (`!True`) or disallow (`!False`) preparation of
the query. By default (`!None`) prepare automatically. See
.. automethod:: execute
:param query: The query to execute.
- :type query: `!str`, `!bytes`, `sql.SQL`, or `sql.Composed`
+ :type query: `~typing.LiteralString`, `!bytes`, `sql.SQL`, `sql.Composed`,
+ or `~string.templatelib.Template`
:param params: The parameters to pass to the query, if any.
+ Can't be specified if ``query`` is a `!Template`.
:type params: Sequence or Mapping
:param prepare: Force (`!True`) or disallow (`!False`) preparation of
the query. By default (`!None`) prepare automatically. See
.. automethod:: executemany
:param query: The query to execute
- :type query: `!str`, `!bytes`, `sql.SQL`, or `sql.Composed`
+ :type query: `~typing.LiteralString`, `!bytes`, `sql.SQL`, or `sql.Composed`
:param params_seq: The parameters to pass to the query
:type params_seq: Sequence of Sequences or Mappings
:param returning: If `!True`, fetch the results of the queries executed
.. automethod:: execute
:param query: The query to execute.
- :type query: `!str`, `!bytes`, `sql.SQL`, or `sql.Composed`
+ :type query: `~typing.LiteralString`, `!bytes`, `sql.SQL`, `sql.Composed`,
+ or `~string.templatelib.Template`
:param params: The parameters to pass to the query, if any.
+ Can't be specified if ``query`` is a `!Template`.
:type params: Sequence or Mapping
:param binary: Specify whether the server should return data in binary
format (`!True`) or in text format (`!False`). By default
.. automethod:: join
+ .. versionchanged:: 3.3
+
+ Added support for `~string.templatelib.Template` sequences.
+ See :ref:`nested template strings <tstring-template-nested>`.
+
.. autoclass:: Identifier
Passing parameters to SQL queries
=================================
+.. note::
+
+ This way of passing queries has been the "classic" way for many years.
+ However, starting from Python 3.14, you might be interested in using the
+ more expressive :ref:`template string queries <template-strings>`.
+
Most of the times, writing a program you will have to mix bits of SQL
statements with values provided by the rest of the program:
.. versionadded:: 3.3
-.. warning::
-
- This is an experimental feature, still `under active development`__ and
- only documented here for preview. Details may change before Psycopg 3.3
- release.
-
- .. __: https://github.com/psycopg/psycopg/pull/1054
-
- Template strings are a Python language feature under active development
- too, planned for release in Python 3.14. Template string queries are
- currently tested in Python 3.14 beta 1.
-
Psycopg can process queries expressed as `template strings`__ defined in
:pep:`750` and implemented for the first time in Python 3.14.
) -> list[User]:
filters = []
if ids is not None:
- filters.append(t"u.id = any({list(ids)})")
+ filters.append(t"u.id = ANY({list(ids)})")
if name_pattern is not None:
filters.append(t"u.name ~* {name_pattern}")
if group_id is not None: