如何在执行插入时覆盖自动增量主键

Ant*_*ain 1 sqlite

在MS SQL中我会使用

SET IDENTITY INSERT ON

我如何在SQLite中做类似的事情.我正在尝试升级数据库,并希望维护原始ID

谢谢

Tho*_*ler 5

您不需要设置IDENTITY INSERT,因为始终可以显式设置该值.使用SQLite,您只需插入ROWID列:

drop table test;
create table test(name varchar);
insert into test(name) values('Hello');
insert into test(rowid, name) values(10, 'World');
select rowid, name from test;
Run Code Online (Sandbox Code Playgroud)

如果您使用自动增量主键,则相同:

drop table test;
create table test(id integer primary key autoincrement, name varchar);
insert into test(name) values('Hello');
insert into test values(10, 'World');
select * from test;
Run Code Online (Sandbox Code Playgroud)

另见http://www.sqlite.org/autoinc.html