aiomysql icon indicating copy to clipboard operation
aiomysql copied to clipboard

aiomysql.sa example fails with "TypeError: 'ResultProxy' object is not iterable" since 0.0.15

Open leon-id opened this issue 6 years ago • 2 comments

Hi! I was trying to run the example from http://aiomysql.readthedocs.io/en/latest/sa.html :

import asyncio
import sqlalchemy as sa

from aiomysql.sa import create_engine


metadata = sa.MetaData()

tbl = sa.Table('tbl', metadata,
               sa.Column('id', sa.Integer, primary_key=True),
               sa.Column('val', sa.String(255)))


@asyncio.coroutine
def go():
    engine = yield from create_engine(user='root', 
                                      db='test_pymysql',
                                      host='127.0.0.1',
                                      password='')

    with (yield from engine) as conn:
        yield from conn.execute(tbl.insert().values(val='abc'))

        res = yield from conn.execute(tbl.select())
        for row in res:
            print(row.id, row.val)

asyncio.get_event_loop().run_until_complete(go())

and got the following:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-4a46c1f5df04> in <module>()
     26             print(row.id, row.val)
     27 
---> 28 asyncio.get_event_loop().run_until_complete(go())

~/miniconda3/lib/python3.6/asyncio/base_events.py in run_until_complete(self, future)
    466             raise RuntimeError('Event loop stopped before Future completed.')
    467 
--> 468         return future.result()
    469 
    470     def stop(self):

<ipython-input-2-4a46c1f5df04> in go()
     23 
     24         res = yield from conn.execute(tbl.select())
---> 25         for row in res:
     26             print(row.id, row.val)
     27 

TypeError: 'ResultProxy' object is not iterable

replacing for row in res: with something like for row in (yield from res.fetchall()): fixes the issue. A little exploration revealed that this error comes since 0.0.15 release and probably since this commit: https://github.com/aio-libs/aiomysql/commit/83252b36e3476779d98059119dec3c4140a7befe so I believe that the example should be brought in line with current code.

leon-id avatar Jun 25 '18 08:06 leon-id

Having the same issue here In python 3.6 using async and await the workaround for now is for row in (await res.fetchall()):

jaapz avatar Aug 22 '18 14:08 jaapz

cursor = await connection.execute(select_report_id) row_info, list_info = {}, [] for row in cursor: for column, value in row.items(): row_info = {**row_info, **{column, value}} list_info.append(row_info) get TypeError: 'ResultProxy' object is not iterable

Tappy95 avatar Feb 27 '20 06:02 Tappy95