Skip to main content

Library for asynchronous connection and execution of queries to the database via ODBC driver

Project description

Library for asynchronous connection and execution of queries to the database via ODBC driver

Software requirements

Install ODBC Driver for your DBMS

On the example of MS SQL: link
For support the async mode on Windows, the version of the driver must support asynchronous execution

Install necessary packages/libraries

On GNU/Linux:

  • g++>=4:8.3.0-1
  • python3-dev=<python_version>
  • python3-pip
  • unixodbc-dev>=2.3.7

The example for Debian:

apt install -y g++ python3-dev python3-pip unixodbc-dev

On Windows:

  • Microsoft Visual C++ compiler version>=14
  • The Windows SDK version>=8
  • ODBC Lib version>=3.81 (It must be included in SDK)

Installation PyAODBC

From pypi.org

pip install pyaodbc

From source

git clone https://github.com/SergioMorelo/pyaodbc.git
cd pyaodbc
python3 setup.py install

Quick Guide

import asyncio
import os
import pyaodbc


async def example():
    dsn = "DRIVER={ODBC Driver 18 for SQL Server};" \
          f"SERVER={os.environ.get('SERVER_ADDRESS')};" \
          f"DATABASE={os.environ.get('DB_NAME')};" \
          f"UID={os.environ.get('USER_NAME')};" \
          f"PWD={{{os.environ.get('USER_PASSWORD')}}};" \
          "TrustServerCertificate=yes;"

    async with pyaodbc.connect(dsn) as conn:
        with conn.cursor() as cur:
            query = """
                select ExampleField = 'Hello World'
                where 1 = ?
            """
            await cur.execute(query, (1, ))
            rows = cur.fetchall()
            return rows


if __name__ == '__main__':
    result = asyncio.run(example())
    print(result)

There's the attribute for control CPU Usage (e.g. for Kubernetes, if your CPU value less than 1) and blocking I/O:

import pyaodbc


pyaodbc._rate = 0.5  # default 1.0
# Than smaller the value, then more operations will be performed by CPU during iterations
# The attribute value will apply to all connections and cursors created with it

async def example():
    dsn = '...'
    async with pyaodbc.connect(dsn) as conn:
        with conn.cursor() as cur:
            ...

Additional information

Additional information on the py-library interface is inside pyaodbc.pyi

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyaodbc-0.2.1.tar.gz (25.9 kB view details)

Uploaded Source

File details

Details for the file pyaodbc-0.2.1.tar.gz.

File metadata

  • Download URL: pyaodbc-0.2.1.tar.gz
  • Upload date:
  • Size: 25.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.0

File hashes

Hashes for pyaodbc-0.2.1.tar.gz
Algorithm Hash digest
SHA256 02b56cb92452dc94488473bc2ff4f01cf29f52fda3a5f6eb29c71da59aa3fe32
MD5 8699144d021bf9afd8a5144494c806a7
BLAKE2b-256 e646d66b0b5b28a2de803914c89266f322da4d5cc1542b702328442de8afd53e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page