如何在异步连接上使用 pandas.read_sql?

问题描述 投票:0回答:1

我正在尝试做异步等效的

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

但它因错误而失败

AttributeError: 'AsyncConnection' object has no attribute 'cursor'
.

完成这项工作的正确方法是什么?

asyn_engine = create_async_engine('sqlite+aiosqlite:///./test.db')
stmt = select(MyTable)
data = pd.read_sql(stmt, async_engine)
pandas asynchronous sqlalchemy orm read-sql
1个回答
3
投票

这个代码在原则上是有效的...

# 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 )
© www.soinside.com 2019 - 2024. All rights reserved.