9951 explained code solutions for 126 technologies


python-mysqlHow can I avoid MySQL IntegrityError when using Python?


To avoid MySQL IntegrityError when using Python, it is important to properly handle exceptions and to create transactions when making changes to the database.

Example code

try:
    # Execute the SQL command
    cursor.execute(sql)
    # Commit your changes in the database
    db.commit()
except IntegrityError as e:
    # Rollback in case there is any error
    db.rollback()

The code above will try to execute the SQL command, and if it encounters an IntegrityError, it will rollback the changes.

Code explanation

  1. try: This will execute the SQL command.
  2. cursor.execute(sql): This will execute the SQL command.
  3. db.commit(): This will commit the changes to the database.
  4. except IntegrityError as e: This will handle the IntegrityError.
  5. db.rollback(): This will rollback the changes in case of an error.

Helpful links

Edit this code on GitHub