我在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)
答案 0 :(得分:5)
从SQLite FAQ我发现:
使用此表,语句
INSERT INTO t1 VALUES(NULL,123);
在逻辑上等同于:
INSERT INTO t1 VALUES((SELECT max(a)FROM t1)+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; }
}
现在效果很好!