← All notes

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

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

Sources

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

Permanent ID: iil:note:2gggsz21

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.},
}

Markdown

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.