0

I have to enter string variable and int variable together in mysql Update statement using python

My code is as:

z=input("enter name:")
u=int(input("enter id"))
sn='update table1 set colname:"%s" where colid=%s'%z
cursor.execute(sn,(u,))
mydb.commit()

In the code I have given above colname should get string value means user name and colid should get int value means id in numbers. But after all these commands I get error:

Error: Not enough arguments for string

I can't understand why I am getting error as for string variable we have to use "%s" in mysql statement and give its value to %z after statement as I have given and for int variable in where clause we have to give its variable in brackets as I have given above.

Note: When I replace colid's %s with a constant integer and remove (u,) from execute statement then the code runs correctly without any errors.

3
  • 1
    You can use f-string as place holders. name = "world" then f"hello {name}" will give you "hello world" Commented Feb 22, 2020 at 15:45
  • 1
    It should be colname=, not colname: Commented Feb 22, 2020 at 16:02
  • Oh sorry I had written it in here by mistake but on my computer I have typed '=' only . But then also it shows error. Commented Feb 22, 2020 at 17:46

1 Answer 1

1

Put both variables in the cursor.execute() call.

sn='update table1 set colname = %s where colid=%s'
cursor.execute(sn, (z, u))
Sign up to request clarification or add additional context in comments.

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.