Python has a standard mechanism for accessing database called python DB-API.
This DB-API also defines a number of error we can face using in the database. Below are some
- InterfaceError – Used for errors in the database module, not the database itself. Must subclass Error.
- DatabaseError – Used for errors in the database. Must subclass Error.
- IntegrityError- This exception is raised when foreign key check fails.
- InternalError- This exception is raised when there is some internal error in MySQL database itself.
- OperationalError- This exception is raised for things that are not in control of the programmer.
This is some of the common error when we use database.
For handling error we use except here.
import mysql.connector try: db_connection = mysql.connector.connect ( host="hostname", user="root", password="pin", database="name_of_database" ) mycursor.execute(SELECT * FROM table) a=mycursor.fetchall() print(a) except: print("Error name")
This is how we can handle error while using databases and SQL.