简体   繁体   中英

Not able to insert INT data in Python using MySQL connector

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.

You need to ensure the last argument is a tuple:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM