Database Handling Errors in Python


There are many sources of errors. A few examples are a syntax error in an executed SQL statement, a connection failure, or calling the fetch method for an already canceled or finished statement handle.

The DB API defines a number of errors that must exist in each database module. The following table lists these exceptions.

Sr.No.Exception & Description
1Warning
Used for non-fatal issues. Must subclass StandardError.
2Error
Base class for errors. Must subclass StandardError.
3InterfaceError
Used for errors in the database module, not the database itself. Must subclass Error.
4DatabaseError
Used for errors in the database. Must subclass Error.
5DataError
Subclass of DatabaseError that refers to errors in the data.
6OperationalError
Subclass of DatabaseError that refers to errors such as the loss of a connection to the database. These errors are generally outside of the control of the Python scripter.
7IntegrityError
Subclass of DatabaseError for situations that would damage the relational integrity, such as uniqueness constraints or foreign keys.
8InternalError
Subclass of DatabaseError that refers to errors internal to the database module, such as a cursor no longer being active.
9ProgrammingError
Subclass of DatabaseError that refers to errors such as a bad table name and other things that can safely be blamed on you.
10NotSupportedError
Subclass of DatabaseError that refers to trying to call unsupported functionality.

Your Python scripts should handle these errors, but before using any of the above exceptions, make sure your MySQLdb has support for that exception. You can get more information about them by reading the DB API 2.0 specification.

Updated on: 31-Jan-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements