如何pandas.read在异步连接上使用www.example.com _sql?

xxhby3vn  于 2023-04-28  发布在  其他
关注(0)|答案(1)|浏览(125)

我正在尝试异步运行

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

to94eoyn1#

原则上,这个代码是有效的……

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

相关问题