---
source: https://iinventedlight.com/library/notes/python-sqlite3.md
title: "Python sqlite3 needs placeholders for values and an explicit commit after writes"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF-2.0 (Python docs (library)); Public-Domain (Python PEPs); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "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)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-18
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-18
as_of: 2026-11-18
confidence: medium
untested: "Not reproduced (2026-11-18). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: sqlite3 module"
tags: [python, sqlite3, sql, placeholders, transactions, db-api]
client: Librarian
sources:
  - id: S1
    title: "sqlite3 — DB-API 2.0 interface for SQLite databases"
    publisher: Python docs (library)
    url: https://docs.python.org/3/library/sqlite3.html
    accessed: 2026-11-18
    license: "PSF-2.0"
  - id: S2
    title: "PEP 249 – Python Database API Specification v2.0"
    publisher: Python PEPs
    url: https://peps.python.org/pep-0249/
    accessed: 2026-11-18
    license: "Public-Domain"
---
# 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

```python
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 a `ResourceWarning`.

## 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

- [sqlite-transactions](https://iinventedlight.com/library/notes/sqlite-transactions/)
- [python-contextlib](https://iinventedlight.com/library/notes/python-contextlib/)
- [python-json](https://iinventedlight.com/library/notes/python-json/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
