Using Textual SQL
SQLAlchemy lets you just use strings, for those cases when the SQL is already known and there isn’t a strong need for the statement to support dynamic features. The text() construct is used to compose a textual statement that is passed to the database mostly unchanged.
It constructs a new TextClause, representing a textual SQL string directly as shown in the below code:
- demo4.py
- from sqlalchemy import text
- from sqlalchemy import create_engine, MetaData
- db_string = "postgresql://postgres:john7810@localhost/testdb"
- engine = create_engine(db_string, echo = True)
- meta = MetaData(bind=engine)
- conn = engine.connect()
- t = text("SELECT * FROM students")
- result = conn.execute(t)
The text() construct supports pre-established bound values using the TextClause.bindparams() method. The parameters can also be explicitly typed as follows:
The text() function also be produces fragments of SQL within a select() object that accepts text() objects as an arguments. The “geometry” of the statement is provided by select() construct , and the textual content by text()construct. We can build a statement without the need to refer to any pre-established Table metadata:
You can also use and_() function to combine multiple conditions in WHERE clause created with the help of text() function:
- from sqlalchemy import and_
- from sqlalchemy.sql import select
- s = select([text("* from students")]) \
- .where(
- and_(
- text("students.name between :x and :y"),
- text("students.id>2")
- )
- )
- conn.execute(s, x = 'A', y = 'L').fetchall()
The alias in SQL corresponds to a “renamed” version of a table or SELECT statement, which occurs anytime you say “SELECT * FROM table1 AS a”. The AS creates a new name for the table. Aliases allow any table or subquery to be referenced by a unique name. In case of a table, this allows the same table to be named in the FROM clause multiple times. It provides a parent name for the columns represented by the statement, allowing them to be referenced relative to this name.
In SQLAlchemy, any Table, select() construct, or other selectable object can be turned into an alias using the From Clause.alias() method, which produces an Alias construct. The alias() function in sqlalchemy.sql module represents an alias, as typically applied to any table or sub-select within a SQL statement using the AS keyword:
- demo5.py
- from sqlalchemy.sql import alias, select
- from sqlalchemy import text
- from sqlalchemy import create_engine, MetaData
- db_string = "postgresql://postgres:john7810@localhost/testdb"
- engine = create_engine(db_string, echo = True)
- meta = MetaData(bind=engine)
- meta.reflect()
- conn = engine.connect()
- students = meta.tables['students']
- st = students.alias("a")
- s = select([st]).where(st.c.id > 2)
- result = conn.execute(s).fetchall()
- print("SELECT result={}".format(result))
Supplement
* SQLAlchemy document - SQL Expression Language Tutorial
* SQLAlchemy document - Reflecting All Tables at Once
沒有留言:
張貼留言