Skip to main content

DB-API 2.0 Interface

DB-API 2.0 compliant interface for dqlite.

Connection

DB-API 2.0 Connection object.

This class provides a PEP 249 compliant interface for dqlite connections. All SQL operations are automatically replicated across the cluster via Raft.

Methods

__enter__(self) -> 'Connection'

__exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None

begin(self) -> None

Begin an explicit transaction.

Starts a transaction block. All subsequent operations will be part of this transaction until commit() or rollback() is called.

Example: conn.begin() cursor.execute("INSERT INTO users (name) VALUES ('Alice')") cursor.execute("INSERT INTO posts (title) VALUES ('Hello')") conn.commit() # Both inserts committed atomically

close(self) -> None

Close the connection.

The connection is unusable after this call.

commit(self) -> None

Commit any pending transaction.

If an explicit transaction was started with BEGIN, this commits it. Otherwise, this is a no-op (dqlite auto-commits individual statements).

cursor(self) -> 'Cursor'

Create a new cursor object using the connection.

Returns: A new Cursor instance

rollback(self) -> None

Roll back any pending transaction.

If an explicit transaction was started with BEGIN, this rolls it back. Otherwise, raises NotSupportedError.

Cursor

DB-API 2.0 Cursor object.

This class provides a PEP 249 compliant interface for executing SQL statements and fetching results.

Properties

description

Column description of the last query result.

Returns a sequence of 7-item sequences, each containing: (name, type_code, display_size, internal_size, precision, scale, null_ok)

For dqlite, we only populate name and set others to None.

lastrowid

Last row ID of an INSERT statement.

rowcount

Number of rows affected by last execute() for DML statements.

Returns -1 if not applicable or not available.

Methods

__enter__(self) -> 'Cursor'

__exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None

close(self) -> None

Close the cursor.

execute(self, operation: str, parameters: Optional[Sequence[Any]] = None) -> 'Cursor'

Execute a database operation (query or command).

Args: operation: SQL statement to execute parameters: Optional sequence of parameters for ? placeholders

Returns: self (for method chaining)

Raises: ProgrammingError: If cursor is closed or SQL is invalid

executemany(self, operation: str, seq_of_parameters: Sequence[Sequence[Any]]) -> 'Cursor'

Execute operation multiple times with different parameters.

Args: operation: SQL statement to execute seq_of_parameters: Sequence of parameter sequences

Returns: self (for method chaining)

Raises: ProgrammingError: If execution fails

fetchall(self) -> list[tuple[typing.Any, ...]]

Fetch all remaining rows of a query result.

Returns: A list of tuples

fetchmany(self, size: Optional[int] = None) -> list[tuple[typing.Any, ...]]

Fetch the next set of rows of a query result.

Args: size: Number of rows to fetch (default: arraysize)

Returns: A list of tuples

fetchone(self) -> Optional[tuple[Any, ...]]

Fetch the next row of a query result set.

Returns: A tuple of column values, or None when no more data is available

setinputsizes(self, sizes: Sequence[Any]) -> None

Predefine memory areas for parameters (no-op for dqlite).

setoutputsize(self, size: int, column: Optional[int] = None) -> None

Set column buffer size for fetches (no-op for dqlite).