How Can I assign a sequence value to a sqlite field when the field UID value is NULL

我的未来我决定 提交于 2020-01-07 05:31:12

问题


How I can assign a sequence value to a field "UID" which is NUll in existing sqlite table, for example

table: FOO  
name    UID
A   1
B   2
C   100
D   NULL
E   NULL
F   NULL

what I want is

table: FOO  
name    UID
A   1
B   2
C   100
D   101
E   102
F   103

Can some body help? I want to seek an alternative for using autoincrement on my own reason...

thanks!


回答1:


Register a function that returns the number of times it's called, and then do

UPDATE FOO SET UID = 100 + increment() WHERE UID IS NULL



回答2:


a drafted approach in my mind, I have no idea if it works fine and how much it reliable......

CREATE TRIGGER auto_next_id
AFTER INSERT ON table FOR EACH ROW
BEGIN
   UPDATE table SET uid = max(uid) + 1 ;
END;


来源:https://stackoverflow.com/questions/3028200/how-can-i-assign-a-sequence-value-to-a-sqlite-field-when-the-field-uid-value-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!