sqlite - 如何使主键从 1000 开始? sqlite.net xamarin android

标签 sqlite xamarin sqlite.net

这个问题在这里已经有了答案:





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仅包含两个字段( nameseq )并且不包含主键或任何索引。 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/

相关文章:

python-3.x - 在函数中打开另一个 .py 文件以在 Python3.5 中传递参数

php - SQLite 文件大小在删除表后仍然没有改变

mysql - 将 Xamarin.forms 与 MySQL 连接

xamarin - 无法解析 altool 输出 : Failed to parse PList data type:/

sqlite - 使用 F# & SQLite.Net.Async PCL 创建数据库表

c# - sqlite-net-pcl 中的 InsertAll 和 UpdateAll VS SQLite.Net-PCL 中的 InsertOrReplaceAll

android - 带有外键的sqlite选择查询另一个表

iphone - core data是否会自动创建SQLite数据库文件?

c++ - QT Sqlite 无法获取行。数据库锁定

xamarin - Xamarin.Forms-如何以编程方式将ActivityIndi​​cator叠加在StackLayout的中间