|
|
面冷心慈的草稿本 · LC3中trap指令的out - CSDN文库· 1 年前 · |
|
|
气宇轩昂的羊肉串 · 民法典笔谈|民法典“特别法人”入法动因、功效 ...· 1 年前 · |
|
|
冷静的葫芦 · 递归遍历树形结构,查找目标元素 - ...· 1 年前 · |
|
|
苦恼的草稿本 · SparkSQL 全面深度解析_spark ...· 1 年前 · |
我正在尝试从一个术语列表中进行一个非常简单的搜索
terms = ['term1', 'term2', 'term3']
如何以编程方式遍历术语列表并从术语列表构造条件,以便可以使用
filter
和
or_
或
_and
进行查询
query.filter(or_(#something constructed from terms))
发布于 2010-04-21 18:21:47
如果您有一个术语列表,并且希望查找字段与其中之一匹配的行,则可以使用
in_()
方法:
terms = ['term1', 'term2', 'term3']
query.filter(Cls.field.in_(terms))
如果你想做一些更复杂的事情,那么
or_()
和
and_()
将
ClauseElement
对象作为参数。SQL及其子类基本上表示查询的ClauseElement
AST
。通常,通过对Column或InstrumentedAttribute对象调用比较运算符来创建子句元素:
# Create the clause element
clause = (users_table.columns['name'] == "something")
# you can also use the shorthand users_table.c.name
# The clause is a binary expression ...
print(type(clause))
# <class 'sqlalchemy.sql.expression._BinaryExpression'>
# ... that compares a column for equality with a bound value.
print(type(clause.left), clause.operator, type(clause.right))
# <class 'sqlalchemy.schema.Column'>, <built-in function eq>,
# <class 'sqlalchemy.sql.expression._BindParamClause'>
# str() compiles it to SQL
print(str(clause))
# users.name = ?
# You can also do that with ORM attributes
clause = (User.name == "something")
print(str(clause))
# users.name = ?
您可以像处理任何Python对象一样处理表示条件的子句元素,将它们放入列表中,将它们组合到其他子句元素中,等等。因此,您可以这样做:
# Collect the separate conditions to a list
conditions = []
for term in terms:
conditions.append(User.name == term)
# Combine them with or to a BooleanClauseList
condition = or_(*conditions)
# Can now use the clause element as a predicate in queries
query = query.filter(condition)
# or to view the SQL fragment
print(str(condition))
# users.name = ? OR users.name = ? OR users.name = ?
发布于 2010-04-21 06:25:03
假设您的
terms
变量包含有效的SQL语句片段,您可以简单地将前面带有星号的
terms
传递给
or_
或
and_
>>> from sqlalchemy.sql import and_, or_
>>> terms = ["name='spam'", "email='[email protected]'"]
>>> print or_(*terms)
name='spam' OR email='[email protected]'
>>> print and_(*terms)
name='spam' AND email='[email protected]'
请注意,这假设
terms
只包含有效且正确转义的SQL片段,因此如果恶意用户能够以某种方式访问
terms
,这可能是不安全的。
与其自己构建SQL片段,不如让SQLAlchemy使用
sqlalchemy.sql
中的其他方法构建参数化的SQL查询。我不知道您是否为表准备了
Table
对象;如果是,假设您有一个名为
users
的变量,它是
Table
的一个实例,它描述数据库中的
users
表。然后,您可以执行以下操作:
from sqlalchemy.sql import select, or_, and_
terms = [users.c.name == 'spam', users.c.email == '[email protected]']
query = select([users], and_(*terms))
for row in conn.execute(query):
# do whatever you want here
在这里,
users.c.name == 'spam'
将创建一个
sqlalchemy.sql.expression._BinaryExpression
对象,该对象记录
users
表的
name
列和包含
spam
的字符串文字之间的二进制相等关系。当您将这个对象转换为字符串时,您将得到一个类似于
users.name = :1
的SQL片段,其中
:1
是参数的占位符。
_BinaryExpression
对象还记得
:1
与
'spam'
的绑定,但是在执行SQL查询之前它不会插入它。当它被插入时,数据库引擎将确保它被正确转义。推荐阅读:
SQLAlchemy's operator paradigm
如果只有数据库表,但没有描述该表的
users
变量,则可以自己创建该表:
from sqlalchemy import Table, MetaData, Column, String, Boolean
metadata = MetaData()
users = Table('users', metadata,
Column('id', Integer, primary_key=True),
|
|
面冷心慈的草稿本 · LC3中trap指令的out - CSDN文库 1 年前 |