How can I use pandas.read_sql on an async connection?

4.2k views Asked by At

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)
1

There are 1 answers

1
Bjoern On

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 )