【问题标题】:SQLite - update command yields null valuesSQLite - 更新命令产生空值
【发布时间】:2014-04-16 07:33:56
【问题描述】:

我在对 SQLite 数据库执行某些更新时遇到了问题。我正在使用适用于 Windows 的 SQLite 3 shell。 我正在运行以下命令:

update resovled_chrom_counts set genus =  
case resolved_name_full  
when resolved_name_full is not null and resolved_name_full != ''  
then substr(resolved_name_full,0,instr(resolved_name_full,' ')-1)  
else  
substr(original_name,0,instr(original_name,' ')-1)  
end;  

它似乎适用于大多数行,但有些只是在其属字段中以空值结束。我尝试使用此表的“id”字段手动检查其中一些。例如,我发现 id='kew-1' 的行在其属字段中为空,并运行以下查询:

select substr(resolved_name_full,0,instr(resolved_name_full,' ')-1)  
from resovled_chrom_counts  
where id='kew-1'; 

令我惊讶的是,我得到了一个结果(不为空)! 看起来查询在“select”语句下有效,但在“update”语句下无效。
谁能给出解释和/或解决方案?
任何帮助,将不胜感激。谢谢!

【问题讨论】:

  • 表中的实际值是多少?
  • 不确定我理解你的意思,但是对于我提到的行(id='kew-1'),resolved_name_full 字段中的值是'Abelmoschus esculentus (L.) Moench',所以当运行'select'语句时我得到'Abelmoschus',但是在执行更新命令时,属字段设置为null。

标签: sqlite


【解决方案1】:

问题不在于substr(resolved_name_full...,而在于CASE。

CASE expression 可以有两种不同的形式:

  1. CASE x WHEN y THEN ...:这会将x 的值与y 的值进行比较。
  2. CASE WHEN a THEN ...:检查a的值是真还是假。

UPDATE语句中的问题是CASE后面直接有一个值(resolved_name_full),所以resolved_name_full的值与表达式resolved_name_full is not null and resolved_name_full != ''的值进行比较,这个比较总是失败,因为resolved_name_full 永远不会是 01

只需使用 CASE 表达式的第二种形式:

update resovled_chrom_counts set genus =
case
when resolved_name_full is not null and resolved_name_full != ''
then substr(resolved_name_full,0,instr(resolved_name_full,' ')-1)
else
substr(original_name,0,instr(original_name,' ')-1)
end;

SQLFiddle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 2012-06-05
    • 2017-09-24
    • 2015-03-22
    相关资源
    最近更新 更多