首页 > 解决方案 > 不是所有参数都用在SQL语句python、MySQL

问题描述

我正在尝试开发这个在 python 中执行的 SQL 评分系统,但在 Sublime 中出现以下错误,有谁知道如何解决和调整任何潜在的未来错误。我已经查看了针对不同堆栈问题解决的类似问题,但找不到我要查找的内容?

Traceback (most recent call last):
  File "C:\Users\User\Desktop\python\demo.py", line 20, in <module>
    mycursor.executemany(sql, val)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\cursor.py", line 668, in executemany
    stmt = self._batch_insert(operation, seq_params)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\cursor.py", line 613, in _batch_insert
    raise errors.ProgrammingError(
mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement
import mysql.connector

db = mysql.connector.connect(
     host="localhost",
     port="3306",
     user="root",
     passwd="root",
     database="trial1"
    )
mycursor = db.cursor()

mycursor.execute("CREATE TABLE trial2 (Name TEXT NULL , Age INT NULL , BPsystolic INT NULL , BPdiastolic INT NULL , ClinicalFeaturesOfTheTIA TEXT NULL , DurationOfSymptoms INT NULL , HistoryOfDiabetes TEXT NULL , ABCD²ScoreForTIA FLOAT NULL )")

sql = "INSERT INTO trial2 (Name, Age, BPsystolic, BPdiastolic, ClinicalFeaturesOfTheTia, DurationOfSymptoms, HistoryOfDiabetes, ABCD²ScoreForTIA) VALUES (%s, %d, %d, %d, %s, %d, %s, %f)"
val = [
  ('Person A', '71', '137', '85', 'Speech disturbance without weakness', '17', 'Yes', None),
  ('Person B', '92', '125', '78', 'Other symptoms', '43', 'Yes', None),
  ('Person C', '27', '130', '90', 'Other symptoms', '34', 'No', None)
]
mycursor.executemany(sql, val)

mydb.commit()

print(mycursor.rowcount, "was inserted.")

sql = "UPDATE trial2 SET ABCD²ScoreForTIA = ((Age >= 60) + (BPsystolic >= 140) + (BPdiastolic >= 90) + case ClinicalFeaturesOfTheTia when 'Unilateral weakness' then 2 when 'Speech disturbance without weakness' then 1 when 'Other symptoms' then 0 end + case when DurationOfSymptoms >= 60 then 2 when DurationOfSymptoms >= 10 then 1 when DurationOfSymptoms < 10 then 0 end + (HistoryOfDiabetes = 'Yes')) / 8 where ABCD²ScoreForTIA is null)"

mycursor.execute(sql)
mydb.commit()
print(mycursor.rowcount, "record(s) affected")

标签: pythonmysqlsql-serverpymysqlpysqlite

解决方案


SQL 语句中未使用所有参数的重复项(Python、MySQL)

第 14 行:更改为

sql = "INSERT INTO trial2 (Name, Age, BPsystolic, BPdiastolic, ClinicalFeaturesOfTheTia, DurationOfSymptoms, HistoryOfDiabetes, ABCD²ScoreForTIA) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"

推荐阅读