在我的Python脚本中,我想使用MySql捕获“我的查询的列数据被截断”警告durnig。
我看到了一些建议以下代码的帖子,但它不起作用。
您知道是否必须导入某些特定模块,或者在使用此代码之前是否应调用某些选项/标志?
谢谢大家
阿菲格
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
模块。例如,warnings.filterwarnings('error', category=MySQLdb.Warning)
将其转变MySQLdb.Warning warnings
为异常(在这种情况下,使用您的try / except可以捕获它们)或'ignore'
根本不显示它们。您可以(也许应该)拥有比类别更多的细粒度过滤器。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句