0

My requirement is to change the name field to sun if id is 1.Below is my code.It throws exception and print error in update?.Is i am doing anything wrong on query or python code?

     import MySQLdb
     import datetime    
        a="sun"
        b=1
        sql="update selva set name=%s where selid=%d"
        try:
            cursor.execute(sql,[a,b])
            db.commit()
        except:
            print "error in update"
            db.rollback()

        db.close()

Any Help will be greatly appreciated!

3
  • Did you try reraising the exception so you can see what it is? Commented Sep 24, 2014 at 6:15
  • i am newbie to python,i found out the exception throws by using print in except block,even i don't know which type of exception throws in my code Commented Sep 24, 2014 at 6:17
  • 1
    Then don't catch it! Remove the try/except and let Python report the exception to you. Commented Sep 24, 2014 at 6:29

3 Answers 3

3

The placeholders in the query should be %s, not %d. I have modified your exception handler to catch and print the exception, and to close the database connection in a finally clause to ensure that it will always be closed.

import MySQLdb
import datetime    
a="sun"
b=1
sql="update selva set name=%s where selid=%s"
try:
    cursor.execute(sql, [a, b])
    db.commit()
except Exception, exc:
    print "error in update"
    print "Exception : %s" % exc
    db.rollback()
finally:
    db.close()
Sign up to request clarification or add additional context in comments.

6 Comments

please use tuple rather list.
@sundarnatarajСундар : tuple or list or any other sequence is fine.
i think for sql we use tuple since they are immutable . even query result are tuples.so it is good practice.please correct me if i am wrong:)
i am getting error near %s since there are no quotes.
The data type of the query result has no bearing on the data type used in cursor.execute. Any sequence is permitted, although a tuple would seem to be the defacto standard.
|
0

Try this:

 import MySQLdb
 import datetime    
    a="sun"
    b=1
    sql="update selva set name = '%s' where selid = '%d'"
    cmd = sql % (a, b)
    try:
        cursor.execute(cmd)
        db.commit()
    except Exception, e:
        print "error in update"
        print repr(e)
        db.rollback()

    db.close()

Tell if there is error message printed out.

6 Comments

@Madhesh What about adding some spaces beside equals. I've update my answer.
You don't want to do it this way which forms the query via string interpolation. Instead you want to use parameterised queries as already present in the OP. The reason: string interpolation is vulnerable to SQL injection attacks whereas parameterised queries are not. Anyway, you need to put single quotes around the name.
error in update OperationalError(1054, "Unknown column 'sun' in 'field list'")
@Madhesh That means you don't have a column named sun, do you?
@mhawke,after putting single quotes in %s,my query works,thanks for help me.
|
0

You can use execute invocation like this

cursor.execute ("update selva set name=%s where selid=%s", (a,b)) #passing a tuple with parameters

or

cursor.execute ("update selva set name='%s' where selid=%d" % (a,b)) #passing a raw sql command with interpoled data 

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.