我喜欢记录在我想调试它们或稍后对它们运行解释计划时发生的所有查询。
例如:
from sqlalchemy import select
from sqlalchemy.dialect import oracle
queries = {}
# ...
sel = select([foo.c.id, foo.c.bar])
queries['foo query'] = sel.compile(dialect=oracle.dialect(),
compile_kwargs={'literal_binds': True})
results = conn.execute(select)
这总是会输出以下警告:
SAWarning: Textual column expression 'id' should be explicitly declared with text('location'), or use column('location') for more specificity (this warning may be suppressed after 10 occurrences) if guess_is_literal else "column"
有什么办法可以抑制这些警告吗?值得注意的是,我只想在记录/打印查询的这种非常特殊的情况下抑制它们,而不是全局抑制。
最佳答案
您可以在 Python 2.7 中抑制 SQL Alchemy 警告消息,如下所示:
import warnings
from sqlalchemy import exc as sa_exc
with warnings.catch_warnings():
warnings.simplefilter("ignore", category=sa_exc.SAWarning)
results = query.execute()
关于python - 使用 stmt.compile 打印查询时如何抑制 SQLAlchemy 警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42635018/