【问题标题】:Make an autoincrement field in SQLite [duplicate]在 SQLite 中创建一个自动增量字段 [重复]
【发布时间】:2014-10-12 23:16:45
【问题描述】:

我在 SQLite 中有一个表

create table table_dishes (_id integer primary key autoincrement, 
    title text,
    price real,
    weight real,
    description text,
    position integer);

如何使字段“位置整数”自增?

【问题讨论】:

  • 签出THIS
  • 你为什么需要这个?你可以认为位置值是_id值,或者如果你想不相等,你可以看到像_id + 1

标签: android sqlite


【解决方案1】:

没有自动增量功能,但他们添加了自动添加的ROWID。因此,您将为您的表创建的整数主键将指向这个ROWID

同样来自SQLITE docs

声明为 INTEGER PRIMARY KEY 的列将自动递增。

【讨论】:

    【解决方案2】:

    根据 SQLite 文档 here

    简短回答:声明为 INTEGER PRIMARY KEY 的列将自动递增

    【讨论】:

      【解决方案3】:

      插入值应该类似于:(SELECT IFNULL(MAX(position), 0) + 1 FROM table_dishes) 对于该列,每次您在table_dishes 表中插入新行时,如果您决定保留position column as non-primary key

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-06-05
        • 1970-01-01
        • 2011-06-01
        • 2018-04-27
        • 2019-06-26
        • 1970-01-01
        • 2017-06-01
        • 1970-01-01
        相关资源
        最近更新 更多