Documentation Home
MySQL Connector/Python Developer Guide
Related Documentation Download this Manual
PDF (US Ltr) - 0.7Mb
PDF (A4) - 0.7Mb


10.12.5 errors.IntegrityError Exception

This exception is raised when the relational integrity of the data is affected. For example, a duplicate key was inserted or a foreign key constraint would fail.

The following example shows a duplicate key error raised as IntegrityError:

Press CTRL+C to copy
cursor.execute("CREATE TABLE t1 (id int, PRIMARY KEY (id))") try: cursor.execute("INSERT INTO t1 (id) VALUES (1)") cursor.execute("INSERT INTO t1 (id) VALUES (1)") except mysql.connector.IntegrityError as err: print("Error: {}".format(err))

errors.IntegrityError is a subclass of errors.DatabaseError.