(Sponsors) Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!
Handling Errors
Updated on Jan 07, 2020
Interacting with a database is an error prone process, so we must always implement some mechanism to handle errors gracefully.
MySQLdb has MySQLdb.Error
exception, a top level exception that can be used to catch all database exception raised by MySQLdb
module.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | from __future__ import print_function
import MySQLdb as my
try:
db = my.connect(host="127.0.0.1",
user="root",
passwd="",
db="world"
)
cursor = db.cursor()
sql = "select * from city"
number_of_rows = cursor.execute(sql)
print(number_of_rows)
db.close()
except my.Error as e:
print(e)
except :
print("Unknown error occurred")
|
Two main error in MySQLdb #
There are two categories of exception class in MySQLdb you need to be aware:
- DatabaseError.
- InterfaceError.
DatabaseError: This exception raises when there is problem in data processing, error in sql syntax, mysql internal problems. If a connection is made and a problem arises then DatabaseError will catch it.
InterfaceError: When database connection fails for some reason, MySQLdb will raise an InterfaceError. Note InterfaceError only get raise when there is internal problem in connection to the database, MySQLdb will not raise InterfaceError because of wrong database name or password.
DatabaseError is further divided into 6 types:
- DataError.
- InternalError.
- IntegrityError.
- OperationalError.
- NotSupportedError.
- ProgrammingError.
DataError: MySQLdb throws this error when there is problem in the data processing, like division by zero, numeric value of of range.
InternalError: This exception is raised when there is some internal error in MySQL database itself. For e.g invalid cursor, transaction out of sync etc.
IntegrityError: This exception is raised when foreign key check fails.
OperationalError: This exception is raised for things that are not in control of the programmer. For e.g unexpected disconnect, error in memory allocation etc, selected database not exists.
NotSupportedError: This exception is raised when there is method or api that is not supported.
ProgrammingError: This exception is raised of programming errors. For e.g table not found, error in mysql syntax, wrong number of parameters specified etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | from __future__ import print_function
import MySQLdb as my
try:
db = my.connect(host="127.0.0.1",
user="root",
passwd="",
db="world"
)
cursor = db.cursor()
sql = "select * from city"
number_of_rows = cursor.execute(sql)
print(number_of_rows)
db.close()
except my.DataError as e:
print("DataError")
print(e)
except my.InternalError as e:
print("InternalError")
print(e)
except my.IntegrityError as e:
print("IntegrityError")
print(e)
except my.OperationalError as e:
print("OperationalError")
print(e)
except my.NotSupportedError as e:
print("NotSupportedError")
print(e)
except my.ProgrammingError as e:
print("ProgrammingError")
print(e)
except :
print("Unknown error occurred")
|
In the next post we discuss how to fetch specific number of rows from the database.
Other Tutorials (Sponsors)
This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!
View Comments