0

Here is what i am trying to do:

    import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  user="root",
  passwd="",
  database="J"
)

mycursor = mydb.cursor()

sql = "UPDATE account SET balance = '100' WHERE address = 'input()'"

mycursor.execute(sql)

mydb.commit()

print(mycursor.rowcount, "record(s) affected")

I want to know how to put input within the SQL command? Any other ways than that? I know in php that you put it within , but in python? And what this code is all about is to refill or decrease customer balance. like in db i got accounts and ofcourse there are alot of customers. How to make it so that it changes according to my input? thanks for reading.

1
  • Store the result in a variable. Use parameter binding as shown here. Do not embed user input in a raw SQL string! Commented May 7, 2020 at 22:03

2 Answers 2

1

Use the placeholder (%s) mechanism:

sql = "UPDATE account SET balance = '100' WHERE address = '%s'"
value = input()
mycursor.execute(sql, (value,)) # note well, the second argument must be a tuple, even if one-element only
Sign up to request clarification or add additional context in comments.

Comments

0
import mysql.connector

mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="J"
)

mycursor = mydb.cursor()
custumValue=input("Enter your Address")
sql = "UPDATE account SET balance = '100' WHERE address = %s"

mycursor.execute(sql,(custumValue,))

mydb.commit()

print(mycursor.rowcount, "record(s) affected")

Comments

Your Answer

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.