import mysql.connector
try:
my_connect = mysql.connector.connect(
host="localhost",
user="",
passwd="",
database="my_tutorial"
)
except mysql.connector.Error as my_error:
print(my_error.msg) # Error message
print(my_error) # With error number
####### end of connection ####
my_cursor = my_connect.cursor()
try:
my_cursor.execute("SELECT * FROM student1 Where class='Five'")
print("Rows returned = ",my_cursor.rowcount)
my_result = my_cursor.fetchone()
print(my_result)
except mysql.connector.Error as my_error:
print(my_error) # Output error details.
my_connect.close()
Output is here
1146 (42S02): Table 'my_tutorial.student1' doesn't exist
import mysql.connector
try:
my_connect = mysql.connector.connect(host="localhost", user="root", passwd="", database="my_tutorial")
my_cursor = my_connect.cursor()
my_cursor.execute("SELECT * FROM unknown_table")
except mysql.connector.Error as err:
if err.errno == 1146: # Table doesn't exist
print("Error: Table not found.")
else:
print(err)
Output:
Error: Table not found.
try:
my_connect = mysql.connector.connect(host="localhost", user="root", passwd="wrong_password")
except mysql.connector.Error as err:
if err.errno == 1045: # Access denied
print("Error: Access denied due to invalid credentials.")
else:
print(err)
Output:
Error: Access denied due to invalid credentials.
try:
my_connect = mysql.connector.connect(host="localhost", user="root", passwd="", database="nonexistent_db")
except mysql.connector.Error as err:
if err.errno == 1049: # Unknown database
print("Error: Database not found.")
else:
print(err)
Output:
Error: Database not found.
try:
my_cursor.execute("SELCT * FROM students") # Typo in SELECT
except mysql.connector.Error as err:
if err.errno == 1064: # SQL syntax error
print("Error: SQL syntax error.")
else:
print(err)
Output:
Error: SQL syntax error.
try:
my_cursor.execute("INSERT INTO students (id, name) VALUES (1, 'John')")
except mysql.connector.Error as err:
if err.errno == 1062: # Duplicate entry
print("Error: Duplicate entry for primary key.")
else:
print(err)
Output:
Error: Duplicate entry for primary key.
try:
my_cursor.execute("INSERT INTO orders (customer_id, order_total) VALUES (999, 250)")
except mysql.connector.Error as err:
if err.errno == 1452: # Foreign key constraint fails
print("Error: Foreign key constraint violation.")
else:
print(err)
Output:
Error: Foreign key constraint violation.
try:
my_connect = mysql.connector.connect(host="localhost", user="root", passwd="", database="my_tutorial", connect_timeout=1)
except mysql.connector.Error as err:
if err.errno == 2003: # Connection timeout error
print("Error: Connection timeout.")
else:
print(err)
Output:
Error: Connection timeout.
try:
my_connect = mysql.connector.connect(host="invalid_host", user="root", passwd="")
except mysql.connector.Error as err:
if err.errno == 2005: # Unknown MySQL server host
print("Error: Unknown MySQL server host.")
else:
print(err)
Output:
Error: Unknown MySQL server host.
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.