Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SQLAlchemy核心查询中的动态限制_Python_Sql_Sqlalchemy - Fatal编程技术网

Python SQLAlchemy核心查询中的动态限制

Python SQLAlchemy核心查询中的动态限制,python,sql,sqlalchemy,Python,Sql,Sqlalchemy,我想写一个查询,在这里我可以动态地将各种限制传递给SQLA核心。例如,我希望能够在SELECTqueryWHERE color='blue'上指定,或者不动态指定。传递一个{'color':'blue'}restrictions dict可能会更好,或者有一种更标准的方法 我已经阅读了教程和API文档,但我想知道: SQLA核心中动态限制的惯用约定是什么?它将如何实现 您可以毫无问题地动态构建查询。例如,您可以执行以下操作: query = select([table]) for key, va

我想写一个查询,在这里我可以动态地将各种限制传递给SQLA核心。例如,我希望能够在
SELECT
query
WHERE color='blue'
上指定,或者不动态指定。传递一个
{'color':'blue'}
restrictions dict可能会更好,或者有一种更标准的方法

我已经阅读了教程和API文档,但我想知道:

SQLA核心中动态限制的惯用约定是什么?它将如何实现



您可以毫无问题地动态构建查询。例如,您可以执行以下操作:

query = select([table])
for key, value in params.iteritems():
    query = query.where(table.columns[key] == value)

print conn.execute(query).fetchall()
其中,
params
只是一个限制字典,例如
{'column':'value'}
。这将生成一个查询,其中所有where子句都与AND链接。当然,如果您需要更复杂的where子句,那么查询的构造可能会有点困难

一个有效的例子:

from sqlalchemy import select
from sqlalchemy import create_engine
from sqlalchemy import Column, String, Integer
from sqlalchemy.schema import MetaData, Table

# create engine and connection
DB_URI = "mysql+mysqldb://username:password@127.0.0.1:3306/database?charset=utf8mb4"
engine = create_engine(DB_URI, convert_unicode=True, echo=False)
conn = engine.connect()

# create table
metadata = MetaData()
pieces_table = Table('pieces', metadata,
    Column('id', Integer, primary_key=True),
    Column('size', String(60)),
    Column('color', String(60)),
)
metadata.create_all(engine)

# insert data
conn.execute(pieces_table.insert(), [
    {'size': 'small', 'color': 'blue'},
    {'size': 'large', 'color': 'blue'},
    {'size': 'small', 'color': 'red'},
])

# query data

def build_query(table, params):
    query = select([table])
    for key, value in params.iteritems():
        query = query.where(table.columns[key] == value)
    return query

params = {
    'size': 'large',
    'color': 'blue',
}
query = build_query(pieces_table, params)
print conn.execute(query).fetchall()

这取决于你在做什么以及如何做。一般来说,没有什么可以阻止您传递
Select
s并根据需要应用。如果您希望使用您描述的dict,您可能会觉得很方便。如果我希望这些查询链接在“或”中,该怎么办?