【发布时间】:2019-10-28 16:40:18
【问题描述】:
我正在尝试使用 InsertAsync 在我的本地数据库(SQLited 数据库)中插入数据。但我不断收到此错误。
sqlite.sqliteexception:sqlite.preparedsqlliteinsertcommand.executenonquery 的约束
以后如何解决和避免这种情况?下面是我的示例代码。
var db = DependencyService.Get<ISQLiteDB>();
var conn = db.GetConnection();
try
{
var caf_insert = new CAFTable
{
CAFNo = caf,
EmployeeID = employeenumber,
CAFDate = DateTime.Parse(date),
CustomerID = retailercode,
StartTime = DateTime.Parse(starttime),
EndTime = DateTime.Parse(endtime),
Photo1 = photo1url,
Photo2 = photo2url,
Photo3 = photo3url,
Video = videourl,
GPSCoordinates = actlocation,
Remarks = remarks,
OtherConcern = otherconcern,
RecordLog = recordlog,
LastUpdated = DateTime.Parse(current_datetime)
};
await conn.InsertOrReplaceAsync(caf_insert);
}
catch (Exception ex)
{
Crashes.TrackError(ex);
}
这是 CAF 表
[Table("tblCaf")]
public class CAFTable
{
[PrimaryKey, MaxLength(50)]
public string CAFNo { get; set; }
[MaxLength(50)]
public string EmployeeID { get; set; }
public DateTime CAFDate { get; set; }
[MaxLength(50)]
public string CustomerID { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public string Photo1 { get; set; }
public string Photo2 { get; set; }
public string Photo3 { get; set; }
public string Video { get; set; }
public string MobilePhoto1 { get; set; }
public string MobilePhoto2 { get; set; }
public string MobilePhoto3 { get; set; }
public string MobileVideo { get; set; }
[MaxLength(2000)]
public string Remarks { get; set; }
[MaxLength(2000)]
public string OtherConcern { get; set; }
[MaxLength(2000)]
public string GPSCoordinates { get; set; }
[MaxLength(100)]
public string RecordLog { get; set; }
public DateTime LastSync { get; set; }
public DateTime LastUpdated { get; set; }
public int Existed { get; set; }
public int Deleted { get; set; }
public int ThisSynced { get; set; }
public int Media1Synced { get; set; }
public int Media2Synced { get; set; }
public int Media3Synced { get; set; }
public int Media4Synced { get; set; }
}
【问题讨论】:
-
主键唯一?
-
CAFTable的表定义是什么? -
@John 我更新了它
-
你的“caf”变量的值是多少?当您将值放入此 sql 查询并在某些查询工具中针对您的 dB 运行它时会发生什么:
select * from caf where cafno = 'value_of_caf_variable'- caf 是主键;您插入的值必须不存在。还要确保您正在查看正确的 dB 文件。作为基于文件的 dB,您的 dev 文件夹中可能有一个副本,您的 bin 文件夹中可能有另一个副本等 - 确保您查看您的应用正在编辑的那个(可能是 bin\debug,而不是项目文件夹中的那个)跨度> -
@CaiusJard 我修好了我正在插入已经存在的保存咖啡馆
标签: c# sqlite xamarin xamarin.forms xamarin.android