Yatish Prasad
Yatish Prasad

Reputation: 77

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

Upvotes: 2

Views: 8712

Answers (1)

mechanical_meat
mechanical_meat

Reputation: 169514

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

Upvotes: 2

Related Questions