5

我在 SQLite 上创建了一个示例表,该表具有一个自动递增的 Id 列。

CREATE TABLE "ESVLIntegration" ("Id" INTEGER PRIMARY KEY  AUTOINCREMENT  NOT NULL  UNIQUE , "ProcessId" TEXT NOT NULL , "UserId" INTEGER NOT NULL , "Status" TEXT NOT NULL , "StartDate" DATETIME NOT NULL , "EndDate" DATETIME, "Operation" TEXT NOT NULL , "SNEquip" TEXT NOT NULL , "CardName" TEXT NOT NULL , "FilePath" TEXT NOT NULL , "Processed" BOOL NOT NULL )

但是当我第二次尝试插入时,出现以下错误:

由于违反约束而中止 PRIMARY KEY 必须是唯一的

这是我的代码

public class ESVLIntegration
{
    public long Id { get; set; }
    public String ProcessId { get; set; }
    public long UserId { get; set; }
    public String Status { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public String Operation { get; set; }
    public String SNEquip { get; set; }
    public String CardName { get; set; }
    public String FilePath { get; set; }
    public Boolean Processed { get; set; }
}

public class Sample : Database<Sample>
{
    public Table<ESVLIntegration> ESVLIntegration { get; set; }
}

private void WriteParameters()
{
    "Writing sample parameters to SQLite DB".LogDebug();
    var pars = new ESVLIntegration();
    pars.ProcessId = Guid.NewGuid().ToString();
    pars.CardName = "gpp3";
    pars.StartDate = DateTime.Now;
    pars.Status = "Start";
    pars.Operation = VerifyStatus;
    pars.SNEquip = "12345";
    pars.FilePath = @"C:\Folder\FilePath";
    pars.Processed = false;
    using (var conn = new SQLiteConnection(connStr))
    {
       conn.Open();
       var db = Sample.Init(conn, 2);
       db.ESVLIntegration.Insert(pars);
    }
}

关于我在这里做错了什么的任何想法?

编辑

SQlite 上的 INTEGER 列的类型为 int64(long)

4

1 回答 1

5

从我发现的 SQLite FAQ 中:

有了这张表,语句

插入 t1 值(NULL,123);

逻辑上等同于说:

插入 t1 值((从 t1 中选择最大值(a))+1,123);

所以我只是将我的班级 ID 更改为可为空

public class ESVLIntegration
{
    public long? Id { get; set; }
    public String ProcessId { get; set; }
    public long UserId { get; set; }
    public String Status { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public String Operation { get; set; }
    public String SNEquip { get; set; }
    public String CardName { get; set; }
    public String FilePath { get; set; }
    public Boolean Processed { get; set; }
}

现在效果很好!

于 2012-08-02T04:42:08.217 回答