sqlite3.IntegrityError
noun
say it: “SEE-kwul-lyte THREE in-TEG-rih-tee AIR-er” /ˈsikwəlˌaɪt θri ɪnˈtɛɡrɪti ˈɛrər/
The Python exception raised by the sqlite3 module when a change would break a rule of the data, such as a repeated value in a UNIQUE column.
What is sqlite3.IntegrityError?
sqlite3.IntegrityError is the exception Python's sqlite3 module raises when a change would break a rule of the
data: a repeated value in a UNIQUE column, a missing NOT NULL value, or a foreign key that points at nothing. Catch it
and raise your own exception, so the code calling yours never reads SQLite's messages.
import sqlite3
conn = sqlite3.connect(":memory:")
conn.execute("CREATE TABLE vendors (id INTEGER PRIMARY KEY, email TEXT NOT NULL UNIQUE)")
conn.execute("INSERT INTO vendors (email) VALUES (?)", ("hilltop@example.com",))
try:
conn.execute("INSERT INTO vendors (email) VALUES (?)", ("hilltop@example.com",))
except sqlite3.IntegrityError as error:
print(error) # UNIQUE constraint failed: vendors.email
In a sentence
- “Signing up the same email twice raised sqlite3.IntegrityError, which Leo turned into DuplicateEmail.”
- “Adding a product for vendor 99, who doesn't exist, raised sqlite3.IntegrityError once foreign keys were on.”
Level: Journeyman · More Python terms at this level
Updated