MySQLdb and big queries using CursorUseResultMixIn
-
29-10-2019 - |
Frage
How can I use the CursorUseResultMixIn
in MySQLdb. The obvious thing to do was:
con=MySQLdb.connect(host='localhost', user='test', passwd='xx', db='yy')
curs = con.cursor(MySQLdb.cursors.CursorUseResultMixIn)
but this gives me this error:
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
What am I doing wrong?
Lösung
The MySQLdb.cursors
module uses CursorUseResultMixIn
like this:
class SSCursor(CursorUseResultMixIn, CursorTupleRowsMixIn,
BaseCursor):
"""This is a Cursor class that returns rows as tuples and stores
the result set in the server."""
That is literally the entire definition of SSCursor
.
Unless you have a specific need to subclass BaseCursor differently, you could just use SSCursor
like this:
import MySQLdb.cursors as cursors
con = MySQLdb.connect(host = 'localhost', user = 'test', passwd = 'xx',
db = 'yy', cursorclass = cursors.SSCursor)
PS. There is also a cursors.SSDictCursor
which returns rows as dictionaries, while storing the result set in the server.
PPS. On Ubuntu 11.10, the MySQLdb.cursors
module is typically located at /usr/lib/pymodules/python2.7/MySQLdb/cursors.py
.
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow