MySQL默认一个连接超过8小时无操作就断开该连接,对于想要长期保持连接的应用场景而言,太不方便
Python的MySQL绑定MySQLdb
发现DBUtils是一个不错的Python连接池 工具,根据这篇文章的代码,运行条SQL insert语句,发现有unicode错误
'latin-1' codec can't encode characters in position 97-99: ordinal not in range(256)
此时需要将原文的连接池构建语句改成
pool = PooledDB(MySQLdb,15,host='localhost',user='test',passwd='test',db='crawl',port=3306, charset="utf8") #15为连接池里的最少连接数
即
多添加 一个参数charset,就能解决unicode错误
其实,从host开始的所有参数,都不是DBUtils关心的,而是透传给MySQLdb这个数据库python绑定接口,怎么透传呢?看PooledDB类的构造函数
| __init__(self, creator, mincached=0, maxcached=0, maxshared=0, maxconnections=0, blocking=False, maxusage=None, setsession=None, reset=True, failures=None, ping=1, *args, **kwargs)
| Set up the DB-API