Вопрос

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?

Related info here and here

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top