Python Glossary Русский

all Python 3sqlite3exception

sqlite3.IntegrityError

exceptions

Raised when the relational integrity of the database is affected, e.g. a foreign key check fails (subclass of sqlite3.DatabaseError).

Syntax

raise sqlite3.IntegrityError

This is a data problem, not a code problem: retrying the same statement produces the same failure, so either fix the incoming data or write INSERT OR IGNORE / ON CONFLICT up front. A SQLite-specific trap: foreign keys are not enforced by default — unless you run PRAGMA foreign_keys = ON on every single connection, a dangling reference is stored silently and IntegrityError never fires.

Examples

import sqlite3
con = sqlite3.connect(':memory:')
con.executescript('CREATE TABLE t(id INTEGER PRIMARY KEY); INSERT INTO t VALUES (1);')
try:
    con.execute('INSERT INTO t VALUES (1)')
except sqlite3.IntegrityError as e:
    print(type(e).__name__)   # → IntegrityError

See also

Open in the full glossarydocs.python.orgReport an inaccuracy