在使用python 对wordpress tag 进行细化代码处理时,遇到了调用MySQLdb模块时的出错,由于错误提示和问题原因相差甚远,查看了N久代码也未发现代码有问题。后来问了下师傅,被告知MySQLdb里有一个断接的坑 ,需要进行数据库重连解决。

一、报错代码及提示

运行出错的代码如下:

import MySQLdb

def getTerm(db,tag):

cursor = db.cursor()

query = "SELECT term_id FROM wp_terms where name=%s "

count = cursor.execute(query,tag)

rows = cursor.fetchall()

db.commit()

#db.close()

if count:

term_id = [int(rows[id][0]) for id in range(count)]

return term_id

else:return None

def addTerm(db,tag):

cursor = db.cursor()

query = "INSERT into wp_terms (name,slug,term_group) values (%s,%s,0)"

data = (tag,tag)

cursor.execute(query,data)

db.commit()

term_id = cursor.lastrowid

sql = "INSERT into wp_term_taxonomy (term_id,taxonomy,description) values (%s,'post_tag',%s) "

value = (term_id,tag)

cursor.execute(sql,value)

db.commit()

db.close()

return int(term_id)

dbconn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='361way', port=3306, charset='utf8', init_command='set names utf8')

tags = ['mysql','1111','aaaa','bbbb','ccccc','php','abc','python','java']

tagids = []

for tag in tags:

termid = getTerm(dbconn,tag)

if termid:

print tag, 'tag id is ',termid

tagids.extend(termid)

else:

termid = addTerm(dbconn,tag)

print 'add tag',tag,'id is ' ,termid

tagids.append(termid)

print 'tag id is ',tagids

直接可以执行,在第for循环里第二次调用getTerm函数时,报错如下:

Traceback (most recent call last):

File "a.py", line 40, in

termid = getTerm(dbconn,tag)

File "a.py", line 11, in getTerm

count = cursor.execute(query,tag)

File "/usr/lib64/python2.6/site-packages/MySQLdb/cursors.py", line 154, in execute

charset = db.character_set_name()

_mysql_exceptions.InterfaceError: (0, '')

二、解决方法

初始时以为是编码问题了,又细核对了几遍未发现编码有问题,在python代码里也未发现异常。后来问过师傅后,师傅来了句提示:

只看代码有啥用,mysql 的超时时间调长点或捕获异常从连,原因是

cursor. connection 没有关闭

但是socket已经断了

cursor 这个行为不会再建立一次socket的

重新执行一次MysqlDB.connect()

看的有点懵懂,先从mysql 里查看了所有timeout相关的变量

mysql> show GLOBAL VARIABLES like "%timeout%";

+----------------------------+-------+

| Variable_name | Value |

+----------------------------+-------+

| connect_timeout | 10 |

| delayed_insert_timeout | 300 |

| innodb_lock_wait_timeout | 50 |

| innodb_rollback_on_timeout | OFF |

| interactive_timeout | 28800 |

| net_read_timeout | 30 |

| net_write_timeout | 60 |

| slave_net_timeout | 3600 |

| table_lock_wait_timeout | 50 |

| wait_timeout | 28800 |

+----------------------------+-------+

10 rows in set (0.00 sec)

发现最小的超时时间是10s ,而我的程序执行起来显然就不了10s 。因为之前查过相关的报错,这里估计这个很可能是另外一个报错:2006,MySQL server has gone away  。即然和这个超时时间应该没关系,那就尝试通过MySQLdb ping测试,如果捕获异常,就再进行重连,修改后的代码为:

#!/usr/bin/python

#coding=utf-8

import MySQLdb

def getTerm(db,tag):

cursor = db.cursor()

query = "SELECT term_id FROM wp_terms where name=%s "

count = cursor.execute(query,tag)

rows = cursor.fetchall()

db.commit()

#db.close()

if count:

term_id = [int(rows[id][0]) for id in range(count)]

print term_id

return term_id

else:return None

def addTerm(db,tag):

cursor = db.cursor()

query = "INSERT into wp_terms (name,slug,term_group) values (%s,%s,0)"

data = (tag,tag)

cursor.execute(query,data)

db.commit()

term_id = cursor.lastrowid

sql = "INSERT into wp_term_taxonomy (term_id,taxonomy,description) values (%s,'post_tag',%s) "

value = (term_id,tag)

cursor.execute(sql,value)

db.commit()

db.close()

return int(term_id)

dbconn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='361way', port=3306, charset='utf8', init_command='set names utf8')

tags = ['mysql','1111','aaaa','bbbb','ccccc','php','abc','python','java']

if __name__ == "__main__":

tagids = []

for tag in tags:

try:

dbconn.ping()

except:

print 'mysql connect have been close'

dbconn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='361way', port=3306, charset='utf8', init_command='set names utf8')

termid = getTerm(dbconn,tag)

if termid:

print tag, 'tag id is ',termid

tagids.extend(termid)

else:

termid = addTerm(dbconn,tag)

print 'add tag',tag,'id is ' ,termid

tagids.append(termid)

print 'All tags id is ',tagids

再执行发现竟然OK了,而细看下结果,发现基本上每1-2次getTerm或addTerm函数调用就会打印一次'mysql connect have been close' 。这里问题虽然已经解决,不过并未细挖到真正的根因。

三、建议

从网上要看的结果来看MySQLdb还有不支持长连接的坑,这个可以通过上面提到的修改my.cnf参数解决,也可以考虑使用其他支持设置timeout时间mysql模块。另外,也建议使用MySQLdb时,可以考虑使用一个对其二次封装的模块torndb ---- A lightweight wrapper around MySQLdb 。

python报错处理_python mysql 断连报错处理相关推荐

  1. python基础课程设计项目_Python+MySQL开发医院网上预约系统(课程设计)一

    一:开发环境的配置 1:桌面环境为cnetos+python2.7 2:MySQL的安装与配置 1)MySQL的安装 wget http://repo.mysql.com/mysql57-commun ...

  2. python医院管理系统代码_Python+MySQL开发医院网上预约系统(课程设计)一

    一:开发环境的配置 1:桌面环境为cnetos7+python2.7 2:mysql的安装与配置 1)mysql的安装 mysql官方文档: mysql yum 下载:或者直接用weget下载: wg ...

  3. 打开mysql 的时候报错_关于mysql的启动报错处理

    昨天使用rpm安装了一个mysql最新版本,在安装的时候因为系统里面自带了mysql5.1的版本.所以我在安装mysql5.5的时候一直报错,故使用yum -y remove mysql*将老版本的安 ...

  4. 断网python第三方库安装_Python离线断网情况下安装numpy、pandas和matplotlib等常用第三方包...

    联网情况下在命令终端CMD中输入"pip install numpy"即可自动安装,pandas和matplotlib同理一样方法进行自动安装. 工作的电脑不能上外网,所以不能通过 ...

  5. python批量查询数据库_Python + MySQL 批量查询百度收录

    做SEO的同学,经常会遇到几百或几千个站点,然后对于收录情况去做分析的情况 那么多余常用的一些工具在面对几千个站点需要去做收录分析的时候,那么就显得不是很合适. 在此特意分享给大家一个批量查询百度收录 ...

  6. python自动备份数据库_Python Mysql自动备份脚本

    测试系统环境  Windows 2003   python 2.5.1  mysql 5.0.1 应该只适用于Win,因为调用了CMD. 增量备份,因为自用,数据库不大. 回头有了需求加上自检测,5天 ...

  7. python登录教务系统_python+mysql实现教务管理系统

    本文实例为大家分享了python实现教务管理系统,供大家参考,具体内容如下 mysql+python构成教务管理系统,提供系统管理员,教职工,学生三级.有注册,添加,修改,发布信息等功能. Login ...

  8. python做数据库管理系统_python+mysql做一个图书管理系统?

    开发一个图书管理系统,首先需要对此项目进行一个简单的需求分析: 主要功能包括:图书信息 图书分类 用户信息 用户借阅统计 管理员 管理员权限 接下来可以进行数据库的设计,在这里我提供一个简单的数据库表 ...

  9. python数据库查询系统_Python MySQL 查询数据(select from)

    1.从表中查询数据(Select From) 要从MySQL中的表中进行选择,请使用"SELECT"语句: 例如: 从"customers"表中选择所有记录,并 ...

最新文章

  1. .NET Framework 4.8发布
  2. Linux系统编程(一)
  3. 如何对oracle数据库进行监控检查
  4. 搜索引擎的那些事(32位MD5算法)
  5. Java树数据结构? [关闭]
  6. 第六届中国云计算大会详细日程
  7. 虚树详解+例子分析+模板
  8. python-rrdtool python-pyrrd
  9. HTML 5 input placeholder 属性 实现搜索框提示文字点击输入后消失
  10. C-从源文件到可执行文件的详细编译链接过程
  11. nodejs操作sqlserver数据_SQL Server数据库损坏和修复
  12. 拒绝Wakelock提高续航!安卓省电优化攻略
  13. Atitit。 《吠陀》 《梨俱吠陀》overview 经读后感  是印度上古时期一些文献的总称
  14. 华为安全HCIP-Security H12-721、H12-722、H12-723题库,含三套vce软件
  15. 股票交易费用精确计算器
  16. 首旅如家新生活方式空间品牌--如咖啡正式落地
  17. 渗透测试工程师(实习生)面试题目
  18. 数据库表关系详解(一对多、一对一、多对多)
  19. 南邮计算机学院考研论坛,考南邮的心得,但愿对大家有所帮助!!!
  20. 如何保护自己的个人隐私

热门文章

  1. PHP经验总结(一)序言
  2. openssl 使用命令
  3. 使用beanUtils操纵javabean
  4. 使用Silverlight for Embedded开发绚丽的界面(1)
  5. oracle 9i生成分析报告,ORACLE 9i 以后的分析函数汇总 - fesing - Qihang.Net
  6. matlab intergral,matlab學習:人臉識別之HOG(Histograms of Oriented Gradients)
  7. python常用的集成开发环境有哪些_python IDE有哪些?哪个好用?
  8. 用户输入和while循环
  9. Next.js踩坑入门系列(七) —— 其他相关知识
  10. php amqp rabbitmq 介绍和使用