Python MySQLdb和使用CursorUseResultMixIn的大查询
如何在MySQLdb中使用Python MySQLdb和使用CursorUseResultMixIn的大查询,python,database,mysql-python,Python,Database,Mysql Python,如何在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
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."""
import MySQLdb.cursors as cursors
con = MySQLdb.connect(host = 'localhost', user = 'test', passwd = 'xx',
db = 'yy', cursorclass = cursors.SSCursor)
这就是SSCursor
的完整定义
除非您特别需要对BaseCursor进行不同的子类划分,否则您可以像这样使用SSCursor
:
class SSCursor(CursorUseResultMixIn, CursorTupleRowsMixIn,
BaseCursor):
"""This is a Cursor class that returns rows as tuples and stores
the result set in the server."""
import MySQLdb.cursors as cursors
con = MySQLdb.connect(host = 'localhost', user = 'test', passwd = 'xx',
db = 'yy', cursorclass = cursors.SSCursor)
另外还有一个cursors.SSDictCursor
,它以字典的形式返回行,同时将结果集存储在服务器中
PPS。在Ubuntu 11.10上,MySQLdb.cursors
模块通常位于/usr/lib/pymodules/python2.7/MySQLdb/cursors.py