2

how to do Update in mysql consider Rate is int(8)

k=int(4000);
db=MySQLdb.connect("localhost","user","pass,"databse")
cursor=db.cursor()
sql="UPDATE tablename SET Rate=k WHERE Name='xxx'
cursor=db.cursor()
try:
 cursor.execute(sql)
 db.commit()
except:
 db.rollback()
db.close()
mechanical_meat
170k25 gold badges238 silver badges231 bronze badges
asked Aug 11, 2016 at 15:20

1 Answer 1

2

sql should be:

sql = "UPDATE tablename SET Rate = (%s) WHERE Name='xxx'"

and then you should do:

cursor.execute(sql,(k,))

note that k needs to be in a container to be passed correctly to .execute(). in this case it is a tuple.

also int(4000) is redundant. you can just do: k = 4000

if you also want to pass in the name you can do:

sql = "UPDATE tablename SET Rate = (%s) WHERE Name=(%s)"

and then:

cursor.execute(sql,(k,name))
answered Aug 11, 2016 at 15:23
Sign up to request clarification or add additional context in comments.

2 Comments

What about numeric data, %g. %s would go for string I guess. Documentation pls. Thanks.
@ankit7540: all data types use %s; that's just how the library works.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.