这个问题在这里已经有了答案:
Set start value for AUTOINCREMENT in SQLite
(7 个回答)
4年前关闭。
我需要主键从 1000 开始。怎么办?
public class LoginTable
{
[PrimaryKey, AutoIncrement, Column("_Id")]
public int id { get; set; }
public string info { get; set; }
public string info2 { get; set; }
}
最佳答案
您可以修改sqlite_sequence
表并分配您想要的任何起点。如果 LoginTable
尚未插入,您需要插入一个新的sqlite_sequence
记录,否则您需要更新现有的 sqlite_sequence
记录。sqlite_sequence
仅包含两个字段( name
和 seq
)并且不包含主键或任何索引。 name
是您的 table 的名称和 seq
是当前使用的序列号,下一个插入将在使用它之前增加它。
例子:
using (var db = new SQLiteConnection("foo.sqlite"))
{
db.CreateTable<LoginTable>();
db.Execute("insert into sqlite_sequence (name, seq) values (?, ?);", new object[] { "LoginTable", 999 });
// if LoginTable records already exist, then the `sqlite_sequence` record exists for this table and you need to update it...
//db.Execute("update sqlite_sequence set seq = ? where name = ?;", new object[] { 999, "LoginTable"});
var login = new LoginTable { info = "info1....", info2 = "info2....." };
db.Insert(login);
db.Insert(login);
db.Insert(login);
foreach (var item in db.Table<LoginTable>())
{
Console.WriteLine(item.id);
}
}
输出:
1000
1001
1002
关于sqlite - 如何使主键从 1000 开始? sqlite.net xamarin android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45624196/