Skip to content

SQLite🔗

Supported drivers:

dbapi default driver connection class
sqlite3 👍 sqlite+sqlite3 sqlite3.Connection

sqlite3🔗

sqlite3 is the default dbapi driver for SQLite in pydapper.

Instalation🔗

sqlite3 is part of the stdlib and thus does not require installing an extra.

pip install pydapper
poetry add pydapper

DSN format🔗

dsn = f"sqlite+sqlite3://{path_to_db}"
dsn = "sqlite+sqlite3://my.db"
dsn = "sqlite://my.db"

Example - connect🔗

Please see the sqlite3 docs for a full description of the context manager behavior.

import pydapper

with pydapper.connect("sqlite://pydapper.db") as commands:
    print(type(commands))
    # <class 'pydapper.sqlite.sqlite3.Sqlite3Commands'>

    print(type(commands.connection))
    # <class 'sqlite3.Connection'>

    # pydapper inherits from `sqlite3.Cursor` in order to supply a context manager
    with commands.cursor() as raw_cursor:
        print(type(raw_cursor))
        # <class 'pydapper.sqlite.sqlite3.Sqlite3Cursor'>
        print(raw_cursor.__class__.__bases__)
        # (<class 'sqlite3.Cursor'>,)

Example - using🔗

Use pydapper with a custom connection pool.

import sqlite3
from collections import deque

import pydapper


class SimplePool:
    """sqlite3 does not provide a pool interface, this is a simple example that should never be used in production"""

    def __init__(self, database: str):
        self._database = database
        self._connections = deque()

    def getconn(self):
        if len(self._connections) == 0:
            return sqlite3.connect(self._database)
        return self._connections.pop()

    def putconn(self, conn):
        self._connections.append(conn)

    def __del__(self):
        for conn in self._connections:
            conn.close()


my_pool = SimplePool("pydapper.db")

commands = pydapper.using(my_pool.getconn())
print(type(commands))
# <class 'pydapper.sqlite.sqlite3.Sqlite3Commands'>

print(type(commands.connection))
# <class 'sqlite3.Connection'>

my_pool.putconn(commands.connection)