我正在尝试做异步等价物
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)
最佳答案
这段代码在原则上是有效的...
# Making pd.read_sql connection the first argument to make it compatible
# with conn.run_syn()
def _read_sql(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 )
关于pandas - 如何在异步连接上使用 pandas.read_sql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70848256/