【问题标题】:Update first n records in SQLite更新 SQLite 中的前 n 条记录
【发布时间】:2016-02-26 19:29:53
【问题描述】:

我想从 Android 应用程序更新我的 SQLite DB 中的前 n 条记录。以下是我迄今为止尝试过的查询。我在order 附近遇到语法错误。

update label set label_is_used = 1 where label_name !='temp' and label_cnt != 0 order by label_cnt desc limit 3
------------------------------------
update label set label_is_used = 1 where label_name !='temp' and label_cnt != 0 order by label_cnt limit 3

docs 开始,这应该符合语法。

我哪里错了?

【问题讨论】:

    标签: android sqlite


    【解决方案1】:

    documentation you linked to 说:

    如果 SQLite 是使用 SQLITE_ENABLE_UPDATE_DELETE_LIMIT 编译时选项构建的,那么 UPDATE 语句的语法会使用可选的 ORDER BY 和 LIMIT 子句进行扩展

    默认情况下该选项未启用,Android 上当然也未启用。

    作为一种解决方法,确定要在子查询中更新的记录:

    UPDATE label
    SET label_is_used = 1
    WHERE label_id IN (SELECT label_id   -- or whatever you use as ID
                       FROM label
                       WHERE label_name != 'temp'
                         AND label_cnt != 0
                       ORDER BY label_cnt DESC
                       LIMIT 3);
    

    【讨论】:

    • 这应该可以。但是,关于如何启用该选项并使我的查询工作的任何想法?好奇的。 :)
    • 哦,只需重新编译并重新安装 Android 操作系统的那部分即可。
    猜你喜欢
    • 2015-03-23
    • 1970-01-01
    • 2016-07-15
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 2023-03-06
    • 2018-06-09
    • 2011-02-19
    相关资源
    最近更新 更多