I
I
isaac-212021-10-16 12:36:16
SQL Server
isaac-21, 2021-10-16 12:36:16

Is there an asynchronous driver for Microsoft SQL Server in python, for sqlalchemy?

I want to try to write asynchronous queries to the database through sqlalchemy, but when I try, it swears that there is no asynchronous driver, please tell me how to solve this problem?
Or what driver to put?

async def async_main():
    connection_string = f"DRIVER={{ODBC Driver 17 for SQL Server}};" \
                        f"SERVER={DB_HOST};DATABASE={DATABASE};" \
                        f"UID={DB_USER};PWD={DB_PASS}"
    connection_url = URL.create("mssql+pyodbc", query={"odbc_connect": connection_string})
    engine = create_async_engine(connection_url,
                                 echo=True)  # при необходимости, включения/выключения логов запросов меняется параметр echo

    async with engine.begin() as conn:
        await conn.run_sync(meta.create_all)

    async with engine.connect() as conn:
        result = await conn.execute(select(CT_DOC_CARDS).where(CT_DOC_CARDS.c.NUMBER_DOC == "BMK2\\173993"))
        print(result.fetchall())

    await engine.dispose()

asyncio.run(async_main())


sqlalchemy.exc.InvalidRequestError: The asyncio extension requires an async driver to be used. The loaded 'pyodbc' is not async.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2021-10-16
@dimonchik2013

write your own and add to aioodbc

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question