Persistence and Serialization Modules

sqlite3

Provides DB-API connections and cursors for SQLite, an embedded relational database. Use parameter placeholders for data values instead of concatenating SQL. Transactions control when writes become durable; a connection context manager manages transactions but does not close the connection. In-memory databases are useful for small tests.

Formal Syntax & Structure

python syntaxpython
import sqlite3

Return Value

module

Module namespace available through import; import is a statement, not a value-returning expression.

Working Example

Example Snippetpython
import sqlite3from contextlib import closingwith closing(sqlite3.connect(":memory:")) as db:    db.execute("CREATE TABLE items (name TEXT)")    db.execute("INSERT INTO items VALUES (?)", ("Tea",))    print(db.execute("SELECT name FROM items").fetchone()[0])
Python Reference