Skip to main content

pydrda

This is a DRDA protocol (https://en.wikipedia.org/wiki/DRDA) database driver.

Currently, we support only DB2.

Requirements

  • Python 3.11+

Installation

$ pip install pydrda

pyDes is required and is installed automatically as a dependency.

Supported Databases

Db2

https://www.ibm.com/analytics/db2

Example

No SSL

import drda

conn = drda.connect(host='serverhost', database='dbname', user='user', password='password', port=xxxxx, timeout=30)
cur = conn.cursor()
cur.execute('select * from foo where name=?', ['alice'])
for r in cur.fetchall():
    print(r[0], r[1])
conn.close()

With SSL connection

import drda

conn = drda.connect(host='serverhost', database='dbname', use_ssl=True, user='user', password='password', port=xxxxx)
cur = conn.cursor()
cur.execute('select * from foo where name=?', ['alice'])
for r in cur.fetchall():
    print(r[0], r[1])
conn.close()

With SSL and CA certificate

To connect to a server with a self-signed certificate or custom CA (such as IBM Db2 on Cloud), pass the server’s CA certificate file path via ssl_client_cert_path:

import drda

conn = drda.connect(host='serverhost', database='dbname', use_ssl=True, ssl_client_cert_path='/path/to/ca-cert.crt', user='user', password='password', port=xxxxx)
cur = conn.cursor()
cur.execute('select * from foo where name=?', ['alice'])
for r in cur.fetchall():
    print(r[0], r[1])
conn.close()

Context Manager and Cursor Iteration

Connections and cursors support Python context managers (with statement), and cursors can be iterated directly without calling fetchall():

import drda

with drda.connect(host='serverhost', database='dbname', user='user', password='password', port=xxxxx) as conn:
    with conn.cursor() as cur:
        cur.execute('select * from foo where name=?', ['alice'])
        for r in cur:
            print(r[0], r[1])

Transactions

Transactions can be explicitly committed or rolled back:

cur.execute("insert into foo values (?, ?)", [1, 'alice'])
conn.commit()

# Or rollback changes:
# conn.rollback()

AsyncIO

import asyncio
import drda.aio

async def main():
    async with await drda.aio.connect(host='serverhost', database='dbname', user='user', password='password', port=xxxxx, timeout=30) as conn:
        async with conn.cursor() as cur:
            await cur.execute('select * from foo where name=?', ['alice'])
            async for r in cur:
                print(r[0], r[1])

asyncio.run(main())

Dictionary Cursor

To fetch query results as Python dictionaries instead of tuples:

import drda

with drda.connect(host='serverhost', database='dbname', user='user', password='password', port=xxxxx) as conn:
    with conn.cursor(drda.DictCursor) as cur:
        cur.execute('select * from foo where name=?', ['alice'])
        for r in cur:
            print(r['ID'], r['NAME'])

For asyncio, use drda.aio.AsyncDictCursor:

import asyncio
import drda.aio

async def main():
    async with await drda.aio.connect(host='serverhost', database='dbname', user='user', password='password', port=xxxxx) as conn:
        async with conn.cursor(drda.aio.AsyncDictCursor) as cur:
            await cur.execute('select * from foo where name=?', ['alice'])
            async for r in cur:
                print(r['ID'], r['NAME'])

asyncio.run(main())

Unit Tests

I have tested the following steps.

Db2

Start Db2 server

$ docker run -itd --name db2 --privileged=true -p 50000:50000 -e LICENSE=accept -e DB2INST1_PASSWORD=password -e DBNAME=testdb --platform=linux/amd64 icr.io/db2_community/db2

Execute test

$ python test_db2.py
$ python test_async_db2.py

Optional environment variables for tests:

  • DB2_HOST (default: localhost)

  • DB2_PORT (default: 50000)

  • DB2_DATABASE (default: testdb)

  • DB2_USER (default: db2inst1)

  • DB2_PASSWORD (default: password)

  • SSL_CLIENT_CERT_PATH (default: None)

Metadata

Release files for pydrda 0.6.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydrda 0.6.4
File Size Uploaded
pydrda-0.6.4.tar.gz 27.0 kB Details

Release files / pydrda-0.6.4.tar.gz

Download URL pydrda-0.6.4.tar.gz
Size 27.0 kB
Tags Source
SHA-256 checksum
How to use checksums
511dc8d6e23d03cac2f8de18aec1449f56c862148c1e3129af31c82e4ff5a053
BLAKE2b-256 checksum
How to use checksums
9787983caf64612e07708bec44191b3edfb6e93a719e8f8329397301ceb06654
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release history Release notifications | RSS feed

This release

0.6.4 This release

1 release file

0.6.3

1 release file

0.6.2

1 release file

0.6.1

1 release file

0.6.0

1 release file

0.5.3

1 release file

0.5.2

1 release file

0.5.1

1 release file

0.5.0

1 release file

0.4.8

1 release file

0.4.7

1 release file

0.4.6

1 release file

0.4.5

1 release file

0.4.4

1 release file

0.4.3

1 release file

0.4.2

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.0

1 release file

0.2.0

1 release file

0.1.0

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page