1

I'm new to Python and especially to SQL.

My goal is:

  1. A user should enter which phone number he wants to change
  2. Then the user should be able to enter the new phone number
  3. This change should then be stored in my MySQL database

As I know the syntax for an update is like this:

sql = "UPDATE table SET fieldname = value" "WHERE fieldname = value"

But if I try to use the code with two variables from an input, it doesn't work:

input_change = input("Write the number to change: ")
input_new = input("Write the new number: ")
sql = "UPDATE table SET telefonnummer = ?" "WHERE telefonnummer = ?"
cursor.execute(sql, (input_change, input_new))
connection.commit()

Does somebody have an idea how I can fix this? Or where can I find a good description about using variables in SQL statements?

Many thanks for the answers.

2
  • What error code do you get? Commented Dec 20, 2020 at 16:13
  • Hello user148, could you try changing this sql = "UPDATE table SET telefonnummer = ?" "WHERE telefonnummer = ?" to sql ='''UPDATE table SET telefonnummer = ? WHERE telefonnummer = ?''' Commented Dec 20, 2020 at 16:15

1 Answer 1

1

Change :

sql = "UPDATE table SET telefonnummer = ?" "WHERE telefonnummer = ?"

to

sql = "UPDATE table SET telefonnummer = ? WHERE telefonnummer = ?"

and

cursor.execute(sql, (input_change, input_new))

to

cursor.execute(sql, [input_change, input_new])
Sign up to request clarification or add additional context in comments.

4 Comments

Cool! This sums it up, I got confused I thought the input() is only one. I think the code looks different in night mode :D
Hello Nir Elbaz & StackOffended Many thanks for your answers. Now I don't get an error anymore, but the update of the phone number does not happen in the database...
It works with this code you had written! :)) I had just to change the sequence here [input_new, input_change]. Thank you very much!
great! If I'm not mistaken making it a tuple or a list I think won't matter , either of the two I think would work as long as it provides the data correspondingly :)

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.