2

In my python code, I want to update the table value as per the user input so i need to pass the dynamic value in the query but i am not able to get any result. Can you suggest me way?

Below is my code:`

import mysql.connector

zone = # Dynamic value

rate = # Dynamic value

conn=mysql.connector.connect(user='root',password='1234',host='localhost',database='demo')


mycursor=conn.cursor()

mycursor.execute("UPDATE interest_rate SET interest=(rate,) where Bank_Name=%s",(zone,))

conn.commit()
`

How to pass the rate in the UPDATE query, I am able to pass the zone variable in the query. Can you help me to pass the rate variable?

I am using python 3.4 and MySQL as the database.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
kajol
  • 41
  • 1
  • 7
  • This question has been [asked before](https://stackoverflow.com/questions/902408/how-to-use-variables-in-sql-statement-in-python) – Zooby Nov 30 '17 at 20:11

1 Answers1

3

You need to use placeholders for all the values and provide all the values as the second argument, as a tuple:

mycursor.execute("UPDATE interest_rate SET interest=%s where Bank_Name=%s", (rate, zone))
Mureinik
  • 297,002
  • 52
  • 306
  • 350