Skip to content

Cursor is not an Iterator #992

@sanchezg

Description

@sanchezg

Describe the bug

Trying to iterate over a set of results:

main_query = cursor.execute(QUERY, args=args)
result = next(main_query)

I get a:
TypeError: 'DictCursor' object is not an iterator

I think the problem is that BaseCursor implements __iter__ method and not the __next__ method, as the Python reference states: https://docs.python.org/3/glossary.html#term-iterator

I made a workaround:

class DataBaseCursor(pymysql.cursors.DictCursor):
    def __iter__(self):
        return self

    def __next__(self):
        return next(iter(self.fetchone, None))

And it works as expected.

I can upload a PR with that for review, what do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions