Skip to content

PRAGMA statements fail with "cannot commit transaction - SQL statements in progress" #199

Open

Description

@merrick-ronan

Observed behaviour

If a PRAGMA statement is executed through the CS50 SQL library, the underlying SQLite command executes successfully, but the library subsequently raises:

sqlite3.OperationalError: cannot commit transaction - SQL statements in progress

Stepping through the implementation shows that the PRAGMA statement returns its result successfully. The exception occurs when the library issues COMMIT.

The apparent cause is that COMMIT is executed before the statement's result has been consumed or the cursor has been finalized.

Expected behaviour

The current behaviour is surprising because the library accepts the statement but ultimately fails with a low-level SQLite error that does not clearly indicate the underlying issue.

If PRAGMA statements are intended to be supported, the library should execute them correctly and return the appropriate result.

If PRAGMA statements are not supported, it would be preferable for the library to reject them explicitly with a clear, high-level exception (or document that they are unsupported) rather than allowing execution to proceed and ultimately failing with an unrelated SQLite error.

Steps to Reproduce

from cs50 import SQL

db = SQL("sqlite:///finance.db")
db.execute("PRAGMA journal_mode")

Activity

  1. givemehat commented on Nov 24, 2026

    @givemehat

    Hi @merrick-ronan and @dmalan,

    I have submitted a fix for this in #200 with unit test coverage:

    1. Detects PRAGMA commands case-insensitively via the first non-comment/whitespace token (since sqlparse classifies PRAGMA as Token.Name rather than a keyword).
    2. Prevents autocommit transaction wrapping (BEGIN/COMMIT) around PRAGMA statements (supporting pragmas like WAL mode and avoiding SQLite unconsumed cursor lockups).
    3. Fetches and coerces result rows prior to commit when statements yield rows, and returns formatted dictionary rows for query PRAGMAs (e.g. table_info, journal_mode, foreign_keys) just like SELECT.
    4. Verified with 23/23 passing unit tests in tests/sql.py.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions