如何在MySQLdb中使用CursorUseResultMixIn
。显而易见的事情是:
con=MySQLdb.connect(host='localhost', user='test', passwd='xx', db='yy')
curs = con.cursor(MySQLdb.cursors.CursorUseResultMixIn)
但这给了我这个错误:
File "C:\Python26\lib\site-packages\MySQLdb\connections.py", line 226, in cursor
return (cursorclass or self.cursorclass)(self)
TypeError: object.__new__() takes no parameters
我做错了什么?
最佳答案
MySQLdb.cursors
模块使用 CursorUseResultMixIn
,如下所示:
class SSCursor(CursorUseResultMixIn, CursorTupleRowsMixIn,
BaseCursor):
"""This is a Cursor class that returns rows as tuples and stores
the result set in the server."""
这实际上就是 SSCursor
的完整定义。
除非您有特定需要以不同方式子类 BaseCursor,否则您可以像这样使用 SSCursor
:
import MySQLdb.cursors as cursors
con = MySQLdb.connect(host = 'localhost', user = 'test', passwd = 'xx',
db = 'yy', cursorclass = cursors.SSCursor)
PS。还有一个 Cursors.SSDictCursor 它将行作为字典返回,同时将结果集存储在服务器中。
PPS。在 Ubuntu 11.10 上,MySQLdb.cursors
模块通常位于 /usr/lib/pymodules/python2.7/MySQLdb/cursors.py
。
关于python - MySQLdb 和使用 CursorUseResultMixIn 的大查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9395793/