Yatish Prasad Yatish Prasad - 3 months ago 16
MySQL Question

UPDATE in mysql using python

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()

Answer

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))
Comments