Python MySQL更新的正确语法?

Python MySQL更新的正确语法?,python,mysql,Python,Mysql,我最近尝试将MySQL更新特性调整为Python MySQL更新特性。我正在尝试修改一个条件语句,但找不到正确的语法。例如,case“a”=“x”then 2,如果有人能看看我的MySQL代码,看看你是否能识别正确的语法,我将不胜感激。 首先,我的Python MySQL: mycursor = db.cursor() mycursor.execute("CREATE TABLE trial7 (Name TEXT NULL , Age INT NULL , BPsystolic I

我最近尝试将MySQL更新特性调整为Python MySQL更新特性。我正在尝试修改一个条件语句,但找不到正确的语法。例如,case“a”=“x”then 2,如果有人能看看我的MySQL代码,看看你是否能识别正确的语法,我将不胜感激。 首先,我的Python MySQL:

mycursor = db.cursor()

mycursor.execute("CREATE TABLE trial7 (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 trial7 (Name, Age, BPsystolic, BPdiastolic, ClinicalFeaturesOfTheTia, DurationOfSymptoms, HistoryOfDiabetes, ABCD²ScoreForTIA) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"
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)

db.commit()

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

sql = "UPDATE trial7 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)
db.commit()
print(mycursor.rowcount, "record(s) affected")
mycursor=db.cursor()
mycursor.execute(“创建表试验7(名称文本NULL、年龄整数NULL、收缩整数NULL、收缩整数NULL、舒张整数NULL、临床特征检查文本NULL、症状持续时间整数NULL、糖尿病历史文本NULL、分数浮点NULL)”)
sql=“将值(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)插入试验7(姓名、年龄、收缩压、舒张压、临床特征、心率、症状持续时间、糖尿病病史、ABCD²评分)
val=[
('Person A'、'71'、'137'、'85'、'Speech Distribution With Visibility'、'17'、'Yes',None),
(‘患者B’、‘92’、‘125’、‘78’、‘其他症状’、‘43’、‘是’、‘无’),
(“患者C”、“27”、“130”、“90”、“其他症状”、“34”、“否”、“无”)
]
mycursor.executemany(sql,val)
db.commit()
打印(mycursor.rowcount,“已插入”)
sql=“更新试验7设置ABCD²评分FORTIA=((年龄>=60)+(收缩压>=140)+(舒张压>=90)+(病例临床特征:当感觉为“单侧无力”时,2当感觉为“无无力的言语障碍”时,1当感觉为“其他症状”时,0结束)+(如果症状持续时间>=60,则为2;如果症状持续时间>=10,则为1;如果症状持续时间<10,则为0结束)+(糖尿病史=‘是’)/8,其中ABCD²ScoreForTIA为空)
mycursor.execute(sql)
db.commit()
打印(mycursor.rowcount,“受影响的记录”)
其次是我收到的错误

3 was inserted.
Traceback (most recent call last):
  File "C:\Users\User\Desktop\python\demo.py", line 28, in <module>
    mycursor.execute(sql)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\cursor.py", line 569, in execute
    self._handle_result(self._connection.cmd_query(stmt))
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\connection.py", line 651, in cmd_query
    result = self._handle_result(self._send_cmd(ServerCmd.QUERY, query))
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\mysql\connector\connection.py", line 538, in _handle_result
    raise errors.get_exception(packet)
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 'then 2) (case ClinicalFeaturesOfTheTiawhen = 'Speech disturbance without weaknes' at line 1
插入了
3。
回溯(最近一次呼叫最后一次):
文件“C:\Users\User\Desktop\python\demo.py”,第28行,在
mycursor.execute(sql)
文件“C:\Users\User\AppData\Local\Programs\Python39\lib\site packages\mysql\connector\cursor.py”,第569行,在execute中
self.\u handle\u result(self.\u connection.cmd\u query(stmt))
cmd\U查询中第651行的文件“C:\Users\User\AppData\Local\Programs\Python39\lib\site packages\mysql\connector\connection.py”
result=self.\u handle\u result(self.\u send\u cmd(ServerCmd.QUERY,QUERY))
文件“C:\Users\User\AppData\Local\Programs\Python39\lib\site packages\mysql\connector\connection.py”,第538行,位于\u handle\u result中
引发错误。获取异常(数据包)
mysql.connector.errors.ProgrammingError:1064(42000):您的SQL语法有错误;查看与MySQL服务器版本对应的手册,以了解在“then 2”(第1行的case ClinicalFeaturesOfTheTiawhen=“无弱点的语音干扰”附近使用的正确语法
最后是我从中派生的MySQL代码

CREATE TABLE `trial`.`trial` ( `Name` TEXT NULL , `Age` INT NULL , `BP systolic` INT NULL , `BP diastolic` INT NULL ,`Clinical features of the TIA` TEXT NULL , 
`Duration of symptoms` INT NULL , `History of diabetes` TEXT NULL , `ABCD² Score for TIA` FLOAT NULL ) ENGINE = InnoDB;

INSERT INTO `trial` (`Name`, `Age`, `BP systolic`, `BP diastolic`, `Clinical features of the TIA`, `Duration of symptoms`, `History of diabetes`, 
`ABCD² Score for TIA`) VALUES ('Person A', '71', '137', '85', 'Speech disturbance without weakness', '17', 'Yes', NULL);
INSERT INTO `trial` (`Name`, `Age`, `BP systolic`, `BP diastolic`, `Clinical features of the TIA`, `Duration of symptoms`, `History of diabetes`, 
`ABCD² Score for TIA`) VALUES ('Person B', '92', '125', '78', 'Other symptoms', '43', 'Yes', NULL);
INSERT INTO `trial` (`Name`, `Age`, `BP systolic`, `BP diastolic`, `Clinical features of the TIA`, `Duration of symptoms`, `History of diabetes`, 
`ABCD² Score for TIA`) VALUES ('Person C', '27', '130', '90', 'Other symptoms', '34', 'No', NULL);

update trial 
set `ABCD² Score for TIA` = ( 
  (Age >= 60) + (`BP systolic` >= 140) + (`BP diastolic` >= 90) +
  case `Clinical features of the TIA`
    when 'Unilateral weakness' then 2 
    when 'Speech disturbance without weakness' then 1 
    when 'Other symptoms' then 0 
  end +  
  case
    when `Duration of symptoms` >= 60 then 2
    when `Duration of symptoms` >= 10 then 1
    when `Duration of symptoms` < 10 then 0
  end + 
  (`History of diabetes` = 'Yes')
) / 8 
where `ABCD² Score for TIA` is null
CREATE TABLE`trial`.`trial`(`Name`TEXT-NULL,`Age`INT-NULL,`BP-systolic`INT-NULL,`BP-舒张期`INT-NULL,`TIA的临床特征`TEXT-NULL,
`症状持续时间`INT NULL,`糖尿病史`TEXT NULL,`ABCD²TIA评分`FLOAT NULL)引擎=InnoDB;
插入“试验”(“名称”,“年龄”,“收缩压”,“舒张压”,“TIA的临床特征”,“症状持续时间”,“糖尿病史”,
`ABCD²TIA`)值的得分('Person A'、'71'、'137'、'85'、'无弱点言语障碍'、'17'、'是'、空);
插入“试验”(“名称”,“年龄”,“收缩压”,“舒张压”,“TIA的临床特征”,“症状持续时间”,“糖尿病史”,
`ABCD²TIA `)值的得分(“患者B”、“92”、“125”、“78”、“其他症状”、“43”、“是”、空);
插入“试验”(“名称”,“年龄”,“收缩压”,“舒张压”,“TIA的临床特征”,“症状持续时间”,“糖尿病史”,
`ABCD²TIA `)值的得分(“患者C”、“27”、“130”、“90”、“其他症状”、“34”、“否”、空);
更新试验
为TIA设置'ABCD²分数`=(
(年龄>=60)+(收缩压>=140)+(舒张压>=90)+
短暂性脑缺血发作的临床特征`
当“单边疲软”时,则2
当“言语障碍无弱点”时,则1
当出现“其他症状”时,则0
完+
案例
当“症状持续时间”>=60时,则为2
当“症状持续时间”>=10时,则为1
当“症状持续时间”小于10时,则为0
完+
(‘糖尿病史’=‘是’)
) / 8 
其中'ABCD²TIA得分'为空

您的
大小写表达式的语法错误。
试试这个:

UPDATE trial7 
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

我不知道你说的mysqli是什么意思,但我想你是指mysqli。拜托clarify@Dharman是的,关于在py文件中编写时语法的变化,我不知道如何解决这个问题是的,这是一个很好的答案。我要在这里添加的唯一一件事是:最好将查询包装在一个多行python字符串中,并带有三个quotes.sql=“”更新试验14集ABCD²评分FORTIA=((年龄>=60)+(收缩压>=140)+(舒张压>=90)+(单侧无力时的病例临床特征异常),然后是“无无力时的言语障碍”时的2,然后是“其他症状”时的1,然后是0结束)+(如果症状持续时间>=60,则为2;如果症状持续时间>=10,则为1;如果症状持续时间<10,则为0结束)+(糖尿病史=“是”)/8其中ABCD²ScoreForTIA为空“”-^SyntaxError:无效syntax@Lamtheram语法错误是什么?发布错误消息的其余部分。正如您在演示中所看到的,我发布的查询在语法上是正确的。@Lamtheram您没有按原样复制和粘贴我的查询。我的代码中没有
ClinicalFeaturesOfTheTiawhen
。@为了表示歉意,这是从e错误的尝试,我发誓我尝试了你的代码,但失败了,但我错了,成功了,谢谢。
> Name     | Age | BPsystolic | BPdiastolic | ClinicalFeaturesOfTheTIA            | DurationOfSymptoms | HistoryOfDiabetes | ABCD²ScoreForTIA
> :------- | --: | ---------: | ----------: | :---------------------------------- | -----------------: | :---------------- | ----------------:
> Person A |  71 |        137 |          85 | Speech disturbance without weakness |                 17 | Yes               |               0.5
> Person B |  92 |        125 |          78 | Other symptoms                      |                 43 | Yes               |             0.375
> Person C |  27 |        130 |          90 | Other symptoms                      |                 34 | No                |              0.25