0

I'm using MySQL connector in Python and trying to insert an integer data, but I keep getting this error:

mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s)' at line 1

My code looks like this:

medDosage = int(''.join(filter(str.isdigit, '42mg')))

myCursor.execute("INSERT INTO dosage (dosageDesc) VALUES (%s)", medDosage)
db.commit()

And this statement has been working just fine for all other variables, and somehow for INT value, it does not work. I tried inserting the string variable instead of int, but doesn't work. Also tried to convert the value such as int(medDosage) to make sure it's the right type, but still doesn't work. I know my syntax is correct so I cannot really understand the error. Can you please help why this error is showing?

Thank you in advance.

1
  • 1
    prefer using myCursor.executemany as being more performant. Commented Dec 8, 2020 at 22:21

1 Answer 1

1

You need to ensure the last argument is a tuple:

myCursor.execute("INSERT INTO dosage (dosageDesc) VALUES (%s)", (medDosage,))
Sign up to request clarification or add additional context in comments.

2 Comments

Omg thank you, I can't believe I wasted so much time on this. I will accept as answer soon as I can (10 min left)
@sarahkim Easy thing to omit, especially with one argument.

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.