In case of error in Query, MySQL returns error messages which can be shown. This error message helps in rectifying the problem.
Here we have used a wrong table name while collecting the records. We used exception handling to capture and display the error message.
Add your userid, password and database name for database connection
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()