trapping a MySql warning(捕获一个 MySql 警告)
问题描述
在我的 python 脚本中,我想在使用 MySql 的查询期间捕获为列 'xxx' 截断的数据"警告.
In my python script I would like to trap a "Data truncated for column 'xxx'" warning durnig my query using MySql.
我看到一些帖子建议使用下面的代码,但它不起作用.
I saw some posts suggesting the code below, but it doesn' work.
您是否知道在使用此代码之前是否必须导入某些特定模块或者是否应该调用某些选项/标志?
Do you know if some specific module must be imported or if some option/flag should be called before using this code?
谢谢大家
阿夫格
import MySQLdb
try:
cursor.execute(some_statement)
# code steps always here: No Warning is trapped
# by the code below
except MySQLdb.Warning, e:
# handle warnings, if the cursor you're using raises them
except Warning, e:
# handle warnings, if the cursor you're using raises them
推荐答案
警告就是:警告.他们被报告给(通常)stderr,但没有做任何其他事情.你不能像异常一样捕捉它们,因为它们没有被引发.
Warnings are just that: warnings. They get reported to (usually) stderr, but nothing else is done. You can't catch them like exceptions because they aren't being raised.
但是,您可以使用 warnings
模块配置警告时要做什么,并关闭它们或将它们变成异常.例如, warnings.filterwarnings('error', category=MySQLdb.Warning)
将 MySQLdb.Warning 警告
转换为异常(在这种情况下,它们将被使用您的 try/except) 或 'ignore'
根本不显示它们.您可以(并且可能应该)拥有比类别更细粒度的过滤器.
You can, however, configure what to do with warnings, and turn them off or turn them into exceptions, using the warnings
module. For instance, warnings.filterwarnings('error', category=MySQLdb.Warning)
to turn MySQLdb.Warning warnings
into exceptions (in which case they would be caught using your try/except) or 'ignore'
to not show them at all. You can (and probably should) have more fine-grained filters than just the category.
这篇关于捕获一个 MySql 警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:捕获一个 MySql 警告
- 更改自动增量起始编号? 2021-01-01
- SQL 临时表问题 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01