Skip to content

Python wrapper for DuckDB to more closely mirror SQLite.

License

Notifications You must be signed in to change notification settings

thesketh/pyduckdb

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pyduckdb

Code style: black

Python wrapper for DuckDB to add type hinting and more closely mirror SQLite. This is still a rough work in progress, but should hopefully highlight the utility of the PEP 249 abstract base classes.

This implementation provides Python type hints, context managers, and more distinct cursor types on top of DuckDB. This is not intended to be used in production, but as a test bed for some ideas and to demonstrate the abstract base classes.

Installation

python3 -mpip install pyduckdb

Usage

Uses the standard Python database API.

from pyduckdb import connect

def main():
    with connect(":memory:") as connection:
        with connection.execute("SELECT 1;") as cursor:
            print(next(cursor))

if __name__ == "__main__":
    main()

There is a very naive async implementation available, which essentially involves wrapping every call with asyncio.to_thread:

import asyncio
from pyduckdb.aiopyduckdb import connect

async def main():
    async with connect(":memory:") as connection:
        async with await connection.execute("SELECT 1;") as cursor:
            print(await cursor.fetchone())

if __name__ == "__main__":
    asyncio.run(main())

Differences from the PEP:

  • Connections implement the execute*() functions from the cursor, and return a cursor, as SQLite does.
  • Connections and Cursors implement executescript() as SQLite does.
  • Cursors implement the same transactional features as their Connections.

About

Python wrapper for DuckDB to more closely mirror SQLite.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages