Capturing error from MySQL database.

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()
Output is here
1146 (42S02): Table 'my_tutorial.student1' doesn't exist
More on Error handling
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer