【问题标题】:How to make a primary key start from 1000? sqlite.net xamarin android [duplicate]如何使主键从 1000 开始? sqlite.net xamarin android [重复]
【发布时间】:2017-08-10 22:13:05
【问题描述】:

我需要主键从1000开始,怎么办?

public class LoginTable
{

    [PrimaryKey, AutoIncrement, Column("_Id")]
    public int id { get; set; }

    public string info { get; set; }
    public string info2 { get; set; }



}

【问题讨论】:

    标签: sqlite xamarin sqlite.net


    【解决方案1】:

    您可以修改sqlite_sequence 表并分配您想要的任何起点。如果LoginTable尚未插入,则需要插入新的sqlite_sequence记录,否则需要更新现有的sqlite_sequence记录。

    sqlite_sequence 仅包含两个字段(name 和 seq),并且不包含主键或任何索引。 name 是您的表的名称,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
    

    【讨论】:

      猜你喜欢
      • 2013-11-17
      • 1970-01-01
      • 1970-01-01
      • 2018-11-23
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多