[英]Not able to insert INT data in Python using MySQL connector
我在 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
我的代碼如下所示:
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,))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.