MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / text

Function text

lib/sqlalchemy/sql/_elements_constructors.py:1616–1692  ·  view source on GitHub ↗

r"""Construct a new :class:`_expression.TextClause` clause, representing a textual SQL string directly. E.g.:: from sqlalchemy import text t = text("SELECT * FROM users") result = connection.execute(t) The advantages :func:`_expression.text` provides o

(text: str)

Source from the content-addressed store, hash-verified

1614
1615@_document_text_coercion("text", ":func:`.text`", ":paramref:`.text.text`")
1616def text(text: str) -> TextClause:
1617 r"""Construct a new :class:`_expression.TextClause` clause,
1618 representing
1619 a textual SQL string directly.
1620
1621 E.g.::
1622
1623 from sqlalchemy import text
1624
1625 t = text("SELECT * FROM users")
1626 result = connection.execute(t)
1627
1628 The advantages :func:`_expression.text`
1629 provides over a plain string are
1630 backend-neutral support for bind parameters, per-statement
1631 execution options, as well as
1632 bind parameter and result-column typing behavior, allowing
1633 SQLAlchemy type constructs to play a role when executing
1634 a statement that is specified literally. The construct can also
1635 be provided with a ``.c`` collection of column elements, allowing
1636 it to be embedded in other SQL expression constructs as a subquery.
1637
1638 Bind parameters are specified by name, using the format ``:name``.
1639 E.g.::
1640
1641 t = text("SELECT * FROM users WHERE id=:user_id")
1642 result = connection.execute(t, {"user_id": 12})
1643
1644 For SQL statements where a colon is required verbatim, as within
1645 an inline string, use a backslash to escape::
1646
1647 t = text(r"SELECT * FROM users WHERE name='\:username'")
1648
1649 The :class:`_expression.TextClause`
1650 construct includes methods which can
1651 provide information about the bound parameters as well as the column
1652 values which would be returned from the textual statement, assuming
1653 it's an executable SELECT type of statement. The
1654 :meth:`_expression.TextClause.bindparams`
1655 method is used to provide bound
1656 parameter detail, and :meth:`_expression.TextClause.columns`
1657 method allows
1658 specification of return columns including names and types::
1659
1660 t = (
1661 text("SELECT * FROM users WHERE id=:user_id")
1662 .bindparams(user_id=7)
1663 .columns(id=Integer, name=String)
1664 )
1665
1666 for id, name in connection.execute(t):
1667 print(id, name)
1668
1669 The :func:`_expression.text` construct is used in cases when
1670 a literal string SQL fragment is specified as part of a larger query,
1671 such as for the WHERE clause of a SELECT statement::
1672
1673 s = select(users.c.id, users.c.name).where(text("id=:user_id"))

Callers 15

test_text_rowcountMethod · 0.90
_assert_and_overrideMethod · 0.90
test_in_22Method · 0.90
test_contains_textMethod · 0.90
test_icontains_textMethod · 0.90
test_startswith_textMethod · 0.90

Calls 1

TextClauseClass · 0.85

Tested by 15

test_text_rowcountMethod · 0.72
_assert_and_overrideMethod · 0.72
test_in_22Method · 0.72
test_contains_textMethod · 0.72
test_icontains_textMethod · 0.72
test_startswith_textMethod · 0.72
test_istartswith_textMethod · 0.72