1

I am using raw sql queries for inserting the data into DB. The insertion is working right, Now I want to perform some checks on this insert query e.g. If the query has inserted the data or not suppose I have insert query like

cursor.execute("some insert query" )

Now I want to know whether cursor.execute has inserted the row then show me some text like success and if it fails to insert for some reason then show me text like error and also if the row is already inserted then show, row already exist.

But I don't know how to perform these checks on cursor.execute.

edit

for i in range(numrows):
                row = cursor.fetchone()
                if row[6]==1:
                        arr["user_id"]=row[0]
                        arr["email"]=row[1]
                        arr["style_quiz_score"]=row[2]
                        arr["style_quiz_answer"]=row[3]
                        arr["date_joined"]=row[4]
                        arr["is_active"]=row[5]
                        arr['firstname'] = row[7]

                        arr["username"]=re.sub(r'[^a-zA-Z0-9]', '_', arr["email"])
                elif row[6]==2:
                        arr['lastname'] = row[7]
        cursor1.execute("insert into auth_user(id,username,first_name,last_name,email,password,is_staff,is_active,is_superuser,date_joined,last_login)  values(%s,%s,%s,%s,%s,'NULL',0,%s,0,%s,0)",[arr["user_id"],arr["username"],arr['firstname'],arr['lastname'],arr["email"],arr["is_active"],arr["date_joined"]])

when i am executing cursor1.execute outside forloop than it insert the last entry , but if i execute it in inside forloop than it gives error and nothing will be inserted

4
  • Maybe this SO question will help you: stackoverflow.com/questions/9014233/… Commented Feb 7, 2013 at 10:47
  • python.org/dev/peps/pep-0249 Commented Feb 7, 2013 at 10:47
  • 1
    I suggest you learn how to do this properly; if you're using Django then you shouldn't be using raw SQL. Django has a nice "models" layer that you should be using. See the docs on the Django web site. Commented Feb 7, 2013 at 11:23
  • 1
    I suggest not using raw SQL for that either, Django's ORM is great for what you're trying to do. But just out of curiosity: what error do you get when inserting in the for loop? Also, I don't see any calls to transaction.commit_unless_managed in your code. Using that may help. Commented Feb 7, 2013 at 13:03

1 Answer 1

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'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.