Python sqlite3 needs placeholders for values and an explicit commit after writes
Answer: Call sqlite3.connect("file.db"), run SQL with ? placeholders, and call con.commit() after INSERT or UPDATE, because the statement implicitly opens a transaction. Always use placeholders instead of string formatting.
Example
import sqlite3
con = sqlite3.connect("tutorial.db")
cur = con.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS movie(title, year, score)")
cur.executemany("INSERT INTO movie VALUES(?, ?, ?)", [("Monty Python and the Holy Grail", 1975, 8.2)])
con.commit()
row = cur.execute("SELECT title FROM movie WHERE year = :y", {"y": 1975}).fetchone()
con.close()
Rules
sqlite3.connect()creates a connection to the database file, implicitly creating it if it does not exist.- An INSERT statement implicitly opens a transaction; changes are saved in the database only after it is committed.
- To avoid SQL injection attacks, bind Python values to SQL statements with placeholders, not string formatting.
- Pass a sequence if unnamed placeholders (
?) are used, and a dict if named placeholders (:name) are used. - Since 3.13, deleting a Connection without calling
close()emits aResourceWarning.
DB-API background
PEP 249 defines paramstyle, a string constant stating the type of parameter marker formatting expected by the interface. qmark means question mark style, e.g. WHERE name=?.
See also
Claims
- id: C1 claim: connect implicitly creates the database file. source: S1 evidence: "implicitly creating it if it does not exist"
- id: C2 claim: INSERT opens a transaction that must be committed. source: S1 evidence: "The INSERT statement implicitly opens a transaction"
- id: C3 claim: Placeholders avoid SQL injection. source: S1 evidence: "Always use placeholders instead of string formatting to bind Python values to SQL statements"
- id: C4 claim: qmark is question mark parameter style. source: S2 evidence: "Question mark style, e.g. ...WHERE name=?"
Sources
- [S1] sqlite3 — DB-API 2.0 interface for SQLite databases. Python docs (library). https://docs.python.org/3/library/sqlite3.html . License: PSF-2.0. Accessed 2026-11-18.
- [S2] PEP 249 – Python Database API Specification v2.0. Python PEPs. https://peps.python.org/pep-0249/ . License: Public-Domain. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python docs (library), sqlite3 — DB-API 2.0 interface for SQLite databases; Python PEPs, PEP 249 – Python Database API Specification v2.0. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Public-Domain (https://peps.python.org/pep-0001/). PEP text is placed in the public domain (some also CC0-1.0). Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Python docs (library), sqlite3 — DB-API 2.0 interface for SQLite databases; Python PEPs, PEP 249 – Python Database API Specification v2.0. PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Public-Domain (https://peps.python.org/pep-0001/). PEP text is placed in the public domain (some also CC0-1.0).
Cite this
iinventedlight. 2026. "Python sqlite3 needs placeholders for values and an explicit commit after writes." iinventedlight Library, iil:note:2gggsz21, version of 2026-11-18. https://iinventedlight.com/library/notes/python-sqlite3/. License: CC-BY-4.0.
@misc{iil_note_2gggsz21,
author = {{iinventedlight}},
title = {{Python sqlite3 needs placeholders for values and an explicit commit after writes}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/python-sqlite3/},
note = {Permanent ID iil:note:2gggsz21. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-sqlite3.md.