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
Yatish Prasad
771 gold badge2 silver badges5 bronze badges
1 Answer 1
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
mechanical_meat
170k25 gold badges238 silver badges231 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
ClimateCalamity
What about numeric data,
%g. %s would go for string I guess. Documentation pls. Thanks.mechanical_meat
@ankit7540: all data types use
%s; that's just how the library works.default