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 compléments
Voir le programme détaillé
Module « sqlalchemy »

Fonction cast - module sqlalchemy

Signature de la fonction cast

def cast(expression: '_ColumnExpressionOrLiteralArgument[Any]', type_: '_TypeEngineArgument[_T]') -> 'Cast[_T]' 

Description

help(sqlalchemy.cast)

Produce a ``CAST`` expression.

:func:`.cast` returns an instance of :class:`.Cast`.

E.g.::

    from sqlalchemy import cast, Numeric

    stmt = select(cast(product_table.c.unit_price, Numeric(10, 4)))

The above statement will produce SQL resembling:

.. sourcecode:: sql

    SELECT CAST(unit_price AS NUMERIC(10, 4)) FROM product

The :func:`.cast` function performs two distinct functions when
used.  The first is that it renders the ``CAST`` expression within
the resulting SQL string.  The second is that it associates the given
type (e.g. :class:`.TypeEngine` class or instance) with the column
expression on the Python side, which means the expression will take
on the expression operator behavior associated with that type,
as well as the bound-value handling and result-row-handling behavior
of the type.

An alternative to :func:`.cast` is the :func:`.type_coerce` function.
This function performs the second task of associating an expression
with a specific type, but does not render the ``CAST`` expression
in SQL.

:param expression: A SQL expression, such as a
 :class:`_expression.ColumnElement`
 expression or a Python string which will be coerced into a bound
 literal value.

:param type\_: A :class:`.TypeEngine` class or instance indicating
 the type to which the ``CAST`` should apply.

.. seealso::

    :ref:`tutorial_casts`

    :func:`.try_cast` - an alternative to CAST that results in
    NULLs when the cast fails, instead of raising an error.
    Only supported by some dialects.

    :func:`.type_coerce` - an alternative to CAST that coerces the type
    on the Python side only, which is often sufficient to generate the
    correct SQL and data coercion.




Vous êtes un professionnel et vous avez besoin d'une formation ? Calcul scientifique
avec Python
Voir le programme détaillé