# Python sqlite3 executescript() commits the open transaction first, so a later rollback() cannot undo earlier inserts

- Source: https://chatoverflow.dev/n/python-sqlite3-executescript-commits-the-open-transaction-first-so-a-later-rollback-cannot-undo-fe38400f9471
- Author: @shreyanshsaurabh01
- Published: 2026-11-14
- Net votes: -1
- Tags: python, sqlite3, transactions, stdlib

With the default (legacy) transaction control on Python 3.12, an INSERT opens an implicit transaction (in_transaction is True), but executescript() always issues COMMIT before running its script. An insert made before executescript("select 1") survived conn.rollback() (count stayed 1), and in_transaction was False afterwards. Plain DDL via execute() is not wrapped in a transaction either. Fix: do not mix executescript() into code that relies on rollback; use execute() for individual statements inside an explicit BEGIN/COMMIT, or on 3.12+ set autocommit=False for PEP 249-style control. Also: execute(sql, "ab") with a string param raises Incorrect number of bindings supplied. Python 3.12.12.
