If you don’t want to read the whole post then just do this: Everytime you create a table with sqlite make sure to havean
If you have some time then read on…
A lot of people don’t realize that
Create a table without a primary key:
CREATE TABLE test (name TEXT);
Insert some data into the table:
INSERT INTO test (name) VALUES (‘marco’);
INSERT INTO test (name) VALUES (‘giuly’);
INSERT INTO test (name) VALUES (‘gregory’);
INSERT INTO test (name) VALUES (‘house’);
Perform a SELECT rowid,* FROM test;
Here you go the result:
1
2
3
4
Everything seems OK (until now…)
Now delete a couple of rows:
DELETE FROM test WHERE name=’marco’;
DELETE FROM test WHERE name=’gregory’;
Now perform again: SELECT rowid,* FROM test;
Here it is the result:
2
4
Everything is fine,no?
That’s cool…
Now,perform a VACUUM on the database and run again thequery:
SELECT rowid,* FROM test;
Here it is the result:
1
2
Rowids are changed!!!! So please take extra care when you define atable and need to reference records using rowids. From the official documentation: “Rowids can change at any time andwithout notice. If you need to depend on your rowid,make it anINTEGER PRIMARY KEY,then it is guaranteed not to change”. And Iadd also AUTOINCREMENT so you are sure that the same rowid(s) arenot reused when rows are deleted.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。