| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
The mssql-python module defines a set of exceptions specified in the Python DB API to handle various database-related errors. Proper error handling is crucial for diagnosing issues and ensuring the robustness of your application.
Exception Classes
The module defines several custom exception classes that are inherited from Python's built-in Exception class. These classes represent different categories of database errors:
Error Code Mapping
When an error occurs, the type of exception raised is determined by the SQLSTATE value provided by the database.
| SQLSTATE | Exception |
|---|---|
| 0A000 | mssql_python.NotSupportedError |
| 40002 | mssql_python.IntegrityError |
| 22*** | mssql_python.DataError |
| 23*** | mssql_python.IntegrityError |
| 24*** | mssql_python.ProgrammingError |
| 25*** | mssql_python.ProgrammingError |
| 42*** | mssql_python.ProgrammingError |
| HYT00 | mssql_python.OperationalError |
| HYT01 | mssql_python.OperationalError |
For instance, a primary key violation (attempting to insert a duplicate key) will raise an IntegrityError.
Here is an example demonstrating how to use the custom exceptions in the mssql-python module:
from mssql_python import connect
from mssql_python.exceptions import DatabaseError, InterfaceError
try:
# Establish a connection
conn = connect("Server=ServerAddress;Database=myDataBase;UID=myUsername;PWD=myPassword;")
# Create a cursor object
cursor = conn.cursor()
# Execute a query
cursor.execute("SELECT * FROM Employees")
# Fetch results
rows = cursor.fetchall()
for row in rows:
print(row)
except DatabaseError as e:
print(f"Database error occurred: {e}")
except InterfaceError as e:
print(f"Interface error occurred: {e}")
finally:
# Close the cursor and connection
cursor.close()
conn.close()| Back | FazBrowse Home | New Git URL |