Coder Social home page Coder Social logo

tonybaloney / aioodbc Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aio-libs/aioodbc

0.0 1.0 0.0 408 KB

aioodbc - is a library for accessing a ODBC databases from the asyncio

License: Apache License 2.0

Python 96.26% Makefile 2.13% Dockerfile 1.61%

aioodbc's Introduction

aioodbc

GitHub Actions status for master branch

image

image

image

Chat on Gitter

aioodbc is a Python 3.7+ module that makes it possible to access ODBC databases with asyncio. It relies on the awesome pyodbc library and preserves the same look and feel. Internally aioodbc employs threads to avoid blocking the event loop, threads are not that as bad as you think!. Other drivers like motor use the same approach.

aioodbc is fully compatible and tested with uvloop. Take a look at the test suite, all tests are executed with both the default event loop and uvloop.

Basic Example

aioodbc is based on pyodbc and provides the same api, you just need to use yield from conn.f() or await conn.f() instead of conn.f()

Properties are unchanged, so conn.prop is correct as well as conn.prop = val.

import asyncio

import aioodbc


async def test_example():
    dsn = "Driver=SQLite;Database=sqlite.db"
    conn = await aioodbc.connect(dsn=dsn)

    cur = await conn.cursor()
    await cur.execute("SELECT 42 AS age;")
    rows = await cur.fetchall()
    print(rows)
    print(rows[0])
    print(rows[0].age)
    await cur.close()
    await conn.close()


asyncio.run(test_example())

Connection Pool

Connection pooling is ported from aiopg and relies on PEP492 features:

import asyncio

import aioodbc


async def test_pool():
    dsn = "Driver=SQLite3;Database=sqlite.db"
    pool = await aioodbc.create_pool(dsn=dsn)

    async with pool.acquire() as conn:
        cur = await conn.cursor()
        await cur.execute("SELECT 42;")
        r = await cur.fetchall()
        print(r)
        await cur.close()
        await conn.close()
    pool.close()
    await pool.wait_closed()


asyncio.run(test_pool())

Context Managers

Pool, Connection and Cursor objects support the context management protocol:

import asyncio

import aioodbc


async def test_example():
    dsn = "Driver=SQLite;Database=sqlite.db"

    async with aioodbc.create_pool(dsn=dsn) as pool:
        async with pool.acquire() as conn:
            async with conn.cursor() as cur:
                await cur.execute("SELECT 42 AS age;")
                val = await cur.fetchone()
                print(val)
                print(val.age)


asyncio.run(test_example())

Installation

In a linux environment pyodbc (hence aioodbc) requires the unixODBC library. You can install it using your package manager, for example:

$ sudo apt-get install unixodbc
$ sudo apt-get install unixodbc-dev

Then:

pip install aioodbc

Run tests

To run tests locally without docker, install unixodbc and sqlite driver:

$ sudo apt-get install unixodbc
$ sudo apt-get install libsqliteodbc

Create virtualenv and install package with requirements:

$ pip install -r requirements-dev.txt

Run tests, lints etc:

$ make fmt
$ make lint
$ make test

Other SQL Drivers

  • aiopg - asyncio client for PostgreSQL
  • aiomysql - asyncio client form MySQL

Requirements

aioodbc's People

Contributors

achimnol avatar alefteris avatar asvetlov avatar bmwant avatar caitinggui avatar dependabot-preview[bot] avatar dependabot[bot] avatar drpoggi avatar duh386 avatar jayantraizada avatar jettify avatar jpz avatar julianit avatar lanfon72 avatar manikanta-mr avatar nickolai-dr avatar peterdotran avatar pyup-bot avatar ra80533 avatar thehesiod avatar vladz avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.