我在Python中使用MySQL连接器,并试图插入一个整数数据,但我一直收到这个错误:
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
我的代码如下:
medDosage = int(''.join(filter(str.isdigit, '42mg')))
myCursor.execute("INSERT INTO dosage (dosageDesc) VALUES (%s)", medDosage)
db.commit()
这个语句对所有其他变量都很有效,但不知何故,对于INT值,它不起作用。我尝试插入字符串变量而不是int,但不起作用。还尝试转换值,如int(medDosage)
,以确保它是正确的类型,但仍然不起作用。我知道我的语法是正确的,所以我不能真正理解这个错误。你能帮我解释为什么会出现这个错误吗?
提前谢谢。
您需要确保最后一个参数是元组:
myCursor.execute("INSERT INTO dosage (dosageDesc) VALUES (%s)", (medDosage,))