Not all parameters were used in the SQL statement (Python, MySQL)(并非所有参数都在 SQL 语句中使用(Python、MySQL))
问题描述
我在以下 Python 代码中遇到错误:
I get an error on the following Python code:
import mysql.connector
cnx = mysql.connector.connect(user='root', password='',
host='127.0.0.1',
database='DB')
cursor = cnx.cursor()
Name = "James"
Department = "Finance"
StartYear = 2001
CurrentPos = 2001
Link = ""
add_user = ("INSERT INTO DB.tbluser "
"(username, department, startyear, currentpos, link) "
"VALUES (%s, %s, %d, %d, %s)")
data_user = (Name, Department, StartYear, CurrentPos, Link)
cursor.execute(add_user, data_user)
cnx.commit()
cursor.close()
cnx.close()
错误信息是
mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement
你明白为什么吗?
推荐答案
参数标记是 %s
而不是 %d
.
The parameter marker is %s
not %d
.
add_user = """INSERT INTO DB.tbluser
(username, department, startyear, currentpos, link)
VALUES (%s, %s, %s, %s, %s)"""
请注意,mysql.connector
使用的参数标记可能看起来与 Python 字符串格式中使用的 %s
相同,但这种关系只是巧合.一些数据库适配器如 oursql
和 sqlite3
使用 ?
作为参数标记而不是 %s
.
Note that the parameter markers used by mysql.connector
may look the same as the %s
used in Python string formatting but the relationship is only coincidental. Some database adapters like oursql
and sqlite3
use ?
as the parameter marker instead of %s
.
这篇关于并非所有参数都在 SQL 语句中使用(Python、MySQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:并非所有参数都在 SQL 语句中使用(Python、MySQL)


基础教程推荐
- 带更新的 sqlite CTE 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01