Batch Upsert Rows into SQLite Transactionally

Apply a batch of stock updates inside one SQLite transaction with validation, ON CONFLICT upserts, and rollback on any bad row.

PythonData Integrity

Code

pythonsnippet
60 lines
from __future__ import annotations

import sqlite3

SCHEMA = """
CREATE TABLE inventory (
    sku TEXT PRIMARY KEY,
    quantity INTEGER NOT NULL CHECK (quantity >= 0),
    price_cents INTEGER NOT NULL CHECK (price_cents >= 0)
)
"""

UPSERT = """
INSERT INTO inventory (sku, quantity, price_cents)
VALUES (?, ?, ?)
ON CONFLICT(sku) DO UPDATE SET
    quantity = excluded.quantity,
    price_cents = excluded.price_cents
"""


def apply_stock_updates(
    connection: sqlite3.Connection,
    rows: list[tuple[str, int, int]],
) -> int:
    for sku, quantity, price_cents in rows:
        if not sku or quantity < 0 or price_cents < 0:
            raise ValueError(f"invalid inventory row: {sku!r}")

    connection.execute("BEGIN IMMEDIATE")
    try:
        cursor = connection.executemany(UPSERT, rows)
        connection.commit()
    except Exception:
        connection.rollback()
        raise
    return cursor.rowcount


def main() -> None:
    connection = sqlite3.connect(":memory:")
    connection.executescript(SCHEMA)

    first = apply_stock_updates(connection, [("A-1", 5, 1200), ("B-2", 3, 800)])
    second = apply_stock_updates(connection, [("A-1", 9, 1150), ("C-3", 1, 450)])
    print("affected rows:", first, second)
    print(connection.execute(
        "SELECT sku, quantity, price_cents FROM inventory ORDER BY sku"
    ).fetchall())

    try:
        apply_stock_updates(connection, [("D-4", -1, 100)])
    except ValueError as exc:
        print(f"rejected: {exc}")
    assert connection.execute("SELECT COUNT(*) FROM inventory").fetchone()[0] == 3


if __name__ == "__main__":
    main()

What the code does

Batch Upsert Rows into SQLite Transactionally is built around the functions apply_stock_updates and main.

for sku, quantity, price_cents in rows is the loop that carries the per-item work. Everything borrowed from __future__ is visible in one line: annotations. ValueError and return appears at a raise inside an except handler — it fires only while that failure type is already being handled.

Points of care

  • Dependency check: __future__ and sqlite3 — not built-in, must be present in the project.
  • The guard not sku or quantity < 0 or price_cents < 0 short-circuits the flow with an early raise when its condition holds.

Applying it

Entry signature: def apply_stock_updates( connection: sqlite3.Connection, rows: list[tuple[str, int, int]], ) -> int

Running the file prints labeled results such as affected rows:, rejected: — each operation’s effect is visible in the output.

The entry point wires apply_stock_updates together — run it once and the whole demo flows.

Statements that matter

except ValueError as exc:
    print(f"rejected: {exc}")
def apply_stock_updates(
connection: sqlite3.Connection,
rows: list[tuple[str, int, int]],
) -> int:
except Exception:
    connection.rollback()
    raise

Interactive Tools for This Topic

FAQ

When is the exception raised?
Inside an `except` handler — the `ValueError` and `return` fires only while that failure type is already being handled, not on its own trigger.
Does this require external packages?
Yes — `__future__` and `sqlite3`. Add them to your project before running the snippet.