Programming with Python

  1. Home
  2. Docs
  3. Programming with Python
  4. Database in Python
  5. Handling Error

Handling Error

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.

syntax:-

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.

Was this article helpful to you? Yes No

How can we help?