简体   繁体   English

无法使用 MySQL 连接器在 Python 中插入 INT 数据

[英]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:我在 Python 中使用 MySQL 连接器并尝试插入 integer 数据,但我不断收到此错误:

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.这个语句对所有其他变量都很好,但对于 INT 值,它不起作用。 I tried inserting the string variable instead of int, but doesn't work.我尝试插入字符串变量而不是 int,但不起作用。 Also tried to convert the value such as int(medDosage) to make sure it's the right type, but still doesn't work.还尝试转换诸如int(medDosage)之类的值以确保它是正确的类型,但仍然不起作用。 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,))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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