重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

Python基础学习教程:python脚本报错如何忽略MySQLdb的warnings

Python基础学习教程:python脚本报错如何忽略MySQLdb的warnings

创新互联建站专注于企业成都营销网站建设、网站重做改版、通山网站定制设计、自适应品牌网站建设、H5网站设计商城网站建设、集团公司官网建设、成都外贸网站建设公司、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为通山等各大城市提供网站开发制作服务。

一、前言

这是在执行python脚本的时候遇到的bug,疯狂的报warnings警告,一坨一坨的,看着实在不雅观,so,还是解决一下比较好。本篇主要讲述的是错误的发现以及如何忽略warnings警告。

报错如下:

test.py:531: Warning: Duplicate entry '11-5' for key 'idx_user_city'
 cursor_build.execute(insert_build_sql)
test.py:531: Warning: Duplicate entry '367-2' for key 'idx_user_city'

根据报错,我们知道错误是因为插入数据的时候主键重复造成的。不过已经用了insert ignore into来插入了,怎么还是会报错呢?

二、为何会抛出warnings

1、抛出错误的原因

后来搜索发现,insert ignore 的插入方式,虽然能避免异常error,但是确实会抛出异常。只是平时没有像这次把warings展示出来了,为什么会展示呢?

Python基础学习教程:python脚本报错如何忽略MySQLdb的warnings

2、mysql的因素

查看mysql的错误级别:

mysql> show variables like '%log_warnings%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_warnings | 1 |
+---------------+-------+
1 row in set, 1 warning (0.00 sec)

根据log_warnings定义:

(1) log_warnings用于标识警告信息是否一并记录到错误日志中。

(2) log_warnings的值为0,表示不记录警告信息。

(3) log_warnings的值为1,表示警告信息一并记录到错误日志中。

(4) log_warnings的值大于1,表示"失败的连接"的信息和创建新连接时"拒绝访问"类的错误信息也会被记录到错误日志中。

由此可见此时的数据库,设置的错误级别是记录warnings的,只是你记录没错,但是抛出来就是你的不对了呀。

三、修改python脚本,实现屏蔽warnings错误的目的

默认的try..except是捕获python脚本中的error的,默认是捕获不到warnings,那么是否可以把warnings转化成error,然后用try..except来捕获呢

1、把warnings转化成error捕获

#import部分
from warnings import filterwarnings
filterwarnings('error', category=MySQLdb.Warning)

打印异常:

try:
 xxxx
except MySQLdb.Warning, w:
sqlWarning = "Warning:%s" % str(w)

把异常转化为error捕获会报错:

Traceback (most recent call last):
 File "test.py", line 548, in 
 main()
 File "test.py", line 533, in main
 cursor_build.execute(insert_build_sql)
 File "cursors.py", line 175, in execute
 if not self._defer_warnings: self._warning_check()
 File "/cursors.py", line 89, in _warning_check
 warn(w[-1], self.Warning, 3)
_mysql_exceptions.Warning: Duplicate entry '11-5' for key 'idx_user_city'

本地测试是没通过,但是觉得这种方式应该是可行的,碍于时间,选择了下面的屏蔽warings方式,不过这种转换warnings为error的思路还是很好地,有兴趣的同学可以尝试下。

2、把warnings 直接忽略:

from warnings import filterwarnings
filterwarnings('ignore', category=MySQLdb.Warning)

下面的try..except中只需要正常捕获异常就行。执行py脚本:

[xxx]$ python2.7 test.py 
01:44:45 py Finished

执行成功,查看数据库,也都成功写入,世界都安静了。

更多的 Python基础学习教程接下来会继续为大家更新!


分享文章:Python基础学习教程:python脚本报错如何忽略MySQLdb的warnings
URL网址:http://cqcxhl.com/article/ghhggi.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP