python - 使用 stmt.compile 打印查询时如何抑制 SQLAlchemy 警告?

标签 python python-2.7 sqlalchemy

我喜欢记录在我想调试它们或稍后对它们运行解释计划时发生的所有查询。

例如:

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/

相关文章:

python - ODOO 10 多对多

python - 导入错误 : DLL load failed: %1 is not a valid Win32 application - paramiko

python - 整数 : "object() takes no parameters" 的 SQLAlchemy 错误

python - 使用双向 association_proxy 时自动传播删除

python - 使用sqlalchemy.sql.functions.char_length作为过滤条件

python - 如何用从另一个文件(数组?)分配的变量替换一个文件中的字符串

python - 如何使用 Python 在 XML 中搜索和替换?

python - 如何将 2D 网格化点云插值到连续区域?

Python字典——如何防止新值覆盖旧值?

python - python3创建scrapy项目的方法