【问题标题】:SQLite conditional insert or replaceSQLite 条件插入或替换
【发布时间】:2013-03-21 13:21:21
【问题描述】:

我正在尝试将记录插入或更新到 sqlite 数据库中,并且仅在新值大于旧值时才更新值。

架构是:

 CREATE table IF NOT EXISTS SearchTable
   (Owner INTEGER PRIMARY KEY, Generations INTEGER DEFAULT 0)

我试过这样的命令:

INSERT OR REPLACE INTO SearchTable(Owner, Generations)
 VALUES (?, MAX((SELECT Generations FROM SearchTable WHERE Owner = ?), ?))

但它给了我世代的空值。

我需要一些帮助。

【问题讨论】:

    标签: android sqlite


    【解决方案1】:

    试试

    INSERT OR REPLACE INTO SearchTable(Owner, Generations)
    SELECT ?, MAX(Generations) FROM SearchTable WHERE Owner = ?
    

    【讨论】:

    • 谢谢彼得。我最终使用: INSERT OR REPLACE INTO SearchTable(Owner, Generations) SELECT ?, MIN(?, COALESCE(Generations, 999)) FROM SearchTable WHERE Owner = ?
    猜你喜欢
    • 1970-01-01
    • 2015-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    相关资源
    最近更新 更多