且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQLite自动增量非主键字段

更新时间:2023-02-12 08:11:48

插入时可以选择max(id)+1.

You can do select max(id)+1 when you do the insertion.

例如:

INSERT INTO Log (id, rev_no, description) VALUES ((SELECT MAX(id) + 1 FROM log), 'rev_Id', 'some description')

INSERT INTO Log (id, rev_no, description) VALUES ((SELECT MAX(id) + 1 FROM log), 'rev_Id', 'some description')

请注意,这将在空表上失败,因为将没有id为0的记录,但是您可以添加第一个虚拟条目或将sql语句更改为此:

Note that this will fail on an empty table since there won't be a record with id is 0 but you can either add a first dummy entry or change the sql statement to this:

INSERT INTO Log (id, rev_no, description) VALUES ((SELECT IFNULL(MAX(id), 0) + 1 FROM Log), 'rev_Id', 'some description')

INSERT INTO Log (id, rev_no, description) VALUES ((SELECT IFNULL(MAX(id), 0) + 1 FROM Log), 'rev_Id', 'some description')