Twisted adbapi的使用
Twisted adbapi没有太多文档。 这段时间我一直在用Twisted +sqlite(3),效果还是不错的。 主要的代码:
from twisted.enterprise import adbapi
_conn = adbapi.ConnectionPool('sqlite3','./NewsDB.db')
def countCB(count): pass #do whatever you want.
_conn.runQuery("select count(*) from news where url='%s'" % url).addCallback(countCB) 今天实验了连接mysql,碰到一些问题,主要是编码的问题。 在执行sql语句,Mysqldb总是报错。 错误信息: ......
File "/usr/local/lib/python2.5/site-packages/MySQL_python-1.2.3c1-py2.5-linux-i686.egg/MySQLdb/cursors.py",line 156,in execute
exceptions.UnicodeEncodeError: 'latin-1' codec can't encode characters in position 115-127: ordinal not in range(256) 判断为弱智Mysqldb把编码总设置为latin-1,一旦sql语句中出现非拉丁语,就会报错。 解决方法也比较简单。 我现在习惯在做事情之前,强制把python内码设置一下:
import sys if sys.getdefaultencoding() != 'utf-8': reload(sys) sys.setdefaultencoding('utf-8') 连接mysql,要这样:
_conn = adbapi.ConnectionPool('MySQLdb',db='test',user='root',passwd='aaaa',host='localhost', use_unicode=True,charset='utf8') 然后应该就可以了。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |