You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PRAGMA statements fail with "cannot commit transaction - SQL statements in progress" #199
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")
I have submitted a fix for this in #200 with unit test coverage:
Detects PRAGMA commands case-insensitively via the first non-comment/whitespace token (since sqlparse classifies PRAGMA as Token.Name rather than a keyword).
Prevents autocommit transaction wrapping (BEGIN/COMMIT) around PRAGMA statements (supporting pragmas like WAL mode and avoiding SQLite unconsumed cursor lockups).
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.
Verified with 23/23 passing unit tests in tests/sql.py.
Activity
givemehat commentedon Nov 24, 2026
Hi @merrick-ronan and @dmalan,
I have submitted a fix for this in #200 with unit test coverage:
PRAGMAcommands case-insensitively via the first non-comment/whitespace token (sincesqlparseclassifiesPRAGMAasToken.Namerather than a keyword).BEGIN/COMMIT) aroundPRAGMAstatements (supporting pragmas like WAL mode and avoiding SQLite unconsumed cursor lockups).table_info,journal_mode,foreign_keys) just likeSELECT.tests/sql.py.