并非所有参数都在 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
.
相关文章