python - 无法使用 MySQL 连接器在 Python 中插入 INT 数据
问题描述
我在 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,))
推荐阅读
- apache-spark - 如何在 spark shell 中启用或获取跟踪 URL?
- .net - 使用 httpwebrequest 调用 ibm watson discovery adddocument api
- php - Campaign Monitor API - 自定义表单
- xaml - UWP 根据屏幕大小调整控件布局/位置
- java - Java如何管理夏令时?
- sql - 对象“ATM_BRANCH”没有文本
- android - 应用程序被 E/com.facebook.internal.AttributionIdentifiers 卡住
- ios - Cordova 访问没有应用程序 ID 的应用程序文件或访问以前的 ID 文件
- python - 在 django 模型中发送和接收信号
- javascript - for...in 与数组输出字符串而不是数字