小编典典

并非在SQL语句中使用了所有参数(Python,MySQL)

mysql

我在以下Python代码上收到错误:

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

你知道为什么吗


阅读 612

收藏
2020-05-17

共1个答案

小编典典

参数标记%s不是%d

add_user = """INSERT INTO DB.tbluser 
              (username, department, startyear, currentpos, link) 
              VALUES (%s, %s, %s, %s, %s)"""

请注意,所使用的参数标记mysql.connector可能看起来与%sPython字符串格式中使用的参数标记相同,但关系只是巧合。一些数据库适配器喜欢oursqlsqlite3使用?的参数标记代替%s

2020-05-17