跳转到主要内容

支持各种数据库的异步操作

项目描述

AIO-Databases

此包为您提供了对一系列数据库(SQLite、PostgreSQL、MySQL)的异步支持。

Tests Status PYPI Version Python Versions

功能

要求

  • python >= 3.9

安装

aio-databases应使用pip安装

$ pip install aio-databases

您必须使用以下命令选择并安装所需的数据库驱动程序

# To support SQLite
$ pip install aio-databases[aiosqlite]  # asyncio

# To support MySQL
$ pip install aio-databases[aiomysql]   # asyncio
$ pip install aio-databases[trio_mysql] # trio

# To support PostgreSQL (choose one)
$ pip install aio-databases[aiopg]      # asyncio
$ pip install aio-databases[asyncpg]    # asyncio
$ pip install aio-databases[triopg]     # trio

# To support ODBC (alpha state)
$ pip install aio-databases[aioodbc]    # asyncio

使用方法

初始化数据库

    from aio_databases import Database

    # Initialize a database
    db = Database('sqlite:///:memory:')  # with default driver

    # Flesh out the driver 
    db = Database('aiosqlite:///:memory:', **driver_params)

设置连接池(可选)

设置连接池

    # Initialize a database's pool
    async def my_app_starts():
        await db.connect()

    # Close the pool
    async def my_app_ends():
        await db.disconnect()

    # As an alternative users are able to use the database
    # as an async context manager

    async with db:
        await my_main_coroutine()

获取连接

    # Acquire and release (on exit) a connection
    async with db.connection():
        await my_code()

    # Acquire a connection only if it not exist
    async with db.connection(False):
        await my_code()

如果已设置连接池,则将使用它

运行SQL查询

    await db.execute('select $1', '1')
    await db.executemany('select $1', '1', '2', '3')

    records = await db.fetchall('select (2 * $1) res', 2)
    assert records == [(4,)]

    record = await db.fetchone('select (2 * $1) res', 2)
    assert record == (4,)
    assert record['res'] == 4

    result = await db.fetchval('select 2 * $1', 2)
    assert result == 4
  • 逐行迭代行
    async for rec in db.iterate('select name from users'):
        print(rec)

管理连接

默认情况下,数据库为查询打开和关闭连接。

    # Connection will be acquired and released for the query
    await db.fetchone('select %s', 42)

    # Connection will be acquired and released again
    await db.fetchone('select %s', 77)

手动打开和关闭连接

    # Acquire a new connection object
    async with db.connection():
        # Only one connection will be used
        await db.fetchone('select %s', 42)
        await db.fetchone('select %s', 77)
        # ...

    # Acquire a new connection or use an existing
    async with db.connection(False):
        # ...

如果已经存在连接,则 db.method 将使用当前的连接

    async with db.connection(): # connection would be acquired here
        await db.fetchone('select %s', 42)  # the connection is used
        await db.fetchone('select %s', 77)  # the connection is used

    # the connection released there

管理事务

    # Start a tranction using the current connection
    async with db.transaction() as trans1:
        # do some work ...

        async with db.transaction() as trans2:
            # do some work ...
            await trans2.rollback()

        # unnessesary, the transaction will be commited on exit from the
        # current context

        await trans1.commit()

    # Create a new connection and start a transaction
    async with db.tranction(True) as trans:
        # do some work ...

错误追踪器

如果您有任何建议、错误报告或烦恼,请向GitHub上的问题追踪器报告。

贡献

项目开发在以下地址进行: https://github.com/klen/aio-databases

许可

许可协议: MIT License

项目详情


下载文件

下载您平台对应的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码发行版

aio_databases-0.16.2.tar.gz (11.9 kB 查看哈希值)

上传时间: 源代码

构建发行版

aio_databases-0.16.2-py3-none-any.whl (16.9 kB 查看哈希值)

上传时间: Python 3

支持者