3

I am trying to do the asynchron equivalent of

engine = create_engine('sqlite:///./test.db')
stmt = session.query(MyTable)
data = pd.read_sql(stmt, engine) 

but it fails with the error AttributeError: 'AsyncConnection' object has no attribute 'cursor'.

What would be the right way to make this work?

asyn_engine = create_async_engine('sqlite+aiosqlite:///./test.db')
stmt = select(MyTable)
data = pd.read_sql(stmt, async_engine)
Bjoern
  • 171
  • 1
  • 11

1 Answers1

4

This code in principal is working...

# Making pd.read_sql_query connection the first argument to make it compatible 
# with conn.run_syn()
def read_sql_query(con, stmt):
    return pd.read_sql(stmt, con)


async def get_df(stmt, engine):
    async with engine.begin() as conn:
        data = await conn.run_sync(_read_sql, stmt)
    return data

asyn_engine = create_async_engine('sqlite+aiosqlite:///./test.db')
stmt = select(MyTable)

data = get_df(stmt, asyn_engine )
Eyad Sibai
  • 811
  • 7
  • 21
Bjoern
  • 171
  • 1
  • 11
  • Thanks for your answer! `pd.read_sql` raise error with asyncpg, use `pd.read_sql_query` instead of that. – rumbarum Oct 27 '22 at 01:07