【问题标题】:SQLite Database Insert Error ConstraintSQLite 数据库插入错误约束
【发布时间】:2013-07-16 19:11:27
【问题描述】:

我正在进行一个 Web 服务调用,它返回一个包含 50 个数据包的消息 bean。然后我按以下方式将它放入我的 SQLite 中

public void DashboardHandler(Array Bean, long cNum)
    {
        foreach (invoiceSummaryBean ib in Bean)
        {
            var dash = new dashboardCustomer
            {
                actualUsage = ib.actualUsage,
                serviceCost = ib.serviceCost,
                mmbtu = ib.mmbtu,
                OptiServiceCost = ib.optimizedServiceCost,
                period = ib.period,
                periodType = ib.periodType,
                service = ib.service,
                serviceType = ib.serviceType,
                measure = ib.unitOfMeasure,                    
                customerNumber = cNum
            };

            try
            {
                db.insertDashboard(dash);
            }
            catch
            {

            }
        }
    }

我的插入方法

public async Task insertDashboard(dashboardCustomer d)
    {
        try
        {
            await db.InsertAsync(d);
        }
        catch(SQLiteException)
        {
            throw;
        }
    }

我的桌子

[Table("dashboardCustomer")]
public class dashboardCustomer
{

    public long customerNumber { get; set; }        
    public int period { get; set; }
    public string periodType { get; set; }
    public string service { get; set; }
    public double actualUsage { get; set; }
    public double mmbtu { get; set; }
    public double OptiServiceCost { get; set; }        
    public double serviceCost {get; set;}        
    public string serviceType { get; set; }        
    public string measure { get; set; }


}

当它尝试插入时会崩溃说 {"Constraint"}?

不确定是什么问题。我需要将所有 50 个数据包都插入到表中。

【问题讨论】:

  • 我不是 100% 确定,但您可能需要将 dashboardCustomer 中的属性标记为 Columns。

标签: c# sqlite windows-store-apps


【解决方案1】:

我发现了这个问题的问题。我不得不改变表的创建方式。奇怪的是每次插入新数据时都必须重新创建它。

【讨论】:

  • 在许多 sqlite 数据库系统中,例如 sqlite-net(不是您使用的脱离语法的系统),您可以执行类似 _connection.CreateTable();这不会创建一个新表,它所做的是确保它存在或者如果它不存在则创建它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-30
  • 1970-01-01
  • 1970-01-01
  • 2013-12-17
  • 2012-06-04
相关资源
最近更新 更多