Reputation: 3505
I am trying to do INSERT operation into the DB using this:
except Exception as e:
print(e)
and, I have an error during INSERT operation to the DB (duplicate username):
(1062, "Duplicate entry 'a' for key 'username_UNIQUE'")
I would like to get just the error message ("Duplicate entry 'a' for key 'username_UNIQUE'"
), but I don't know how to take the second field of the tuple. If I try e[1]
or e(1)
I just get another error. How do I take this second field from the tuple.
Upvotes: 0
Views: 547
Reputation: 27196
You are using MySQL. Therefore the exception is an instance of IntegrityError (from mysql.connector.errors).
Therefore:
from mysql.connector.errors import IntegrityError
try:
# execute some SQL here
except IntegrityError as e:
print(e.msg)
Upvotes: 3
Reputation: 3505
I solved it by using:
eval(str(e))[1]
However, I would like to have a more simple solution, if anyone happens to know. Thanks.
Upvotes: 0