Skip to content

rows_where() raises OperationalError when offset is given without limit #816

Description

@ikatyal2110

Passing offset to rows_where() (or pks_and_rows_where()) without also passing limit generates invalid SQL and raises OperationalError.

Repro:

import sqlite_utils

db = sqlite_utils.Database(":memory:")
db["t"].insert_all([{"id": i} for i in range(5)])
list(db["t"].rows_where(offset=2))
# OperationalError: near "2": syntax error

Root cause: In db.py rows_where() appends OFFSET N to the SQL only when limit is not None is False, but SQLite requires LIMIT to appear before OFFSET. The generated SQL is SELECT * FROM "t" OFFSET 2, which is invalid syntax.

The same issue affects search_sql() when offset is given without limit.

Fix: When offset is set but limit is not, emit LIMIT -1 before the OFFSET clause. SQLite treats a negative limit as "no upper bound", so LIMIT -1 OFFSET N correctly returns all rows starting from position N.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions