Participer au site avec un Tip
Rechercher
 

Améliorations / Corrections

Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.

Emplacement :

Description des améliorations :

Vous êtes un professionnel et vous avez besoin d'une formation ? Programmation Python
Les fondamentaux
Voir le programme détaillé
Classe « ColumnOperators »

Méthode sqlalchemy.ColumnOperators.contains

Signature de la méthode contains

def contains(self, other: 'Any', **kw: 'Any') -> 'ColumnOperators' 

Description

help(ColumnOperators.contains)

Implement the 'contains' operator.

Produces a LIKE expression that tests against a match for the middle
of a string value:

.. sourcecode:: sql

    column LIKE '%' || <other> || '%'

E.g.::

    stmt = select(sometable).where(sometable.c.column.contains("foobar"))

Since the operator uses ``LIKE``, wildcard characters
``"%"`` and ``"_"`` that are present inside the <other> expression
will behave like wildcards as well.   For literal string
values, the :paramref:`.ColumnOperators.contains.autoescape` flag
may be set to ``True`` to apply escaping to occurrences of these
characters within the string value so that they match as themselves
and not as wildcard characters.  Alternatively, the
:paramref:`.ColumnOperators.contains.escape` parameter will establish
a given character as an escape character which can be of use when
the target expression is not a literal string.

:param other: expression to be compared.   This is usually a plain
  string value, but can also be an arbitrary SQL expression.  LIKE
  wildcard characters ``%`` and ``_`` are not escaped by default unless
  the :paramref:`.ColumnOperators.contains.autoescape` flag is
  set to True.

:param autoescape: boolean; when True, establishes an escape character
  within the LIKE expression, then applies it to all occurrences of
  ``"%"``, ``"_"`` and the escape character itself within the
  comparison value, which is assumed to be a literal string and not a
  SQL expression.

  An expression such as::

    somecolumn.contains("foo%bar", autoescape=True)

  Will render as:

  .. sourcecode:: sql

    somecolumn LIKE '%' || :param || '%' ESCAPE '/'

  With the value of ``:param`` as ``"foo/%bar"``.

:param escape: a character which when given will render with the
  ``ESCAPE`` keyword to establish that character as the escape
  character.  This character can then be placed preceding occurrences
  of ``%`` and ``_`` to allow them to act as themselves and not
  wildcard characters.

  An expression such as::

    somecolumn.contains("foo/%bar", escape="^")

  Will render as:

  .. sourcecode:: sql

    somecolumn LIKE '%' || :param || '%' ESCAPE '^'

  The parameter may also be combined with
  :paramref:`.ColumnOperators.contains.autoescape`::

    somecolumn.contains("foo%bar^bat", escape="^", autoescape=True)

  Where above, the given literal parameter will be converted to
  ``"foo^%bar^^bat"`` before being passed to the database.

.. seealso::

    :meth:`.ColumnOperators.startswith`

    :meth:`.ColumnOperators.endswith`

    :meth:`.ColumnOperators.like`




Vous êtes un professionnel et vous avez besoin d'une formation ? Sensibilisation à
l'Intelligence Artificielle
Voir le programme détaillé