[Answered ]-How to check whether query have inserted data or not in Python

2👍

Assuming you’re using Django (you’re not specific about it in your question, but you’re using the django tag), you need to do transaction.commit_unless_managed() (from django.db import transaction) after issuing the insert query with cursor.execute.

You can check for exceptions when calling commit_unless_managed to see if the insert went well or not:

from django.db import connection, transaction, DatabaseError, IntegrityError

cursor = connection.cursor()
cursor.execute("some insert query" )

try:
    transaction.commit_unless_managed()
except DatabaseError, IntegrityError:
    print 'error'
else:
    print 'success'

Leave a comment