【问题标题】:Join operation in SQLite in Xamarin form app在 Xamarin 表单应用程序中加入 SQLite 操作
【发布时间】:2017-04-24 16:03:47
【问题描述】:

我是 Xamarin 表单的新手。我创建了两个表并尝试对这两个表执行联接操作,但我无法加入该表。

我已经浏览了这些链接,但没有成功

Link1Link2

谁能帮助我如何在 Xamarin 表单中加入两个表。下面是我的 JOIN 操作代码

public Thoughts GetQueryForAllRecords(int ID)
{
    var q = connection.Query<Thoughts>("SELECT Thought,CreatedOn, AssignedToName, AssignedOn FROM Thoughts JOIN AssignedThought ON Thoughts.ID = AssignedThought.ID where Thoughts.ID=?;", ID);

    return q.Last(t => t.ID == ID);
}

但上面的代码只返回 'Thoughts' 表记录,而不是来自 'AssignedThought' 表。

请提出我在这里做错了什么。

【问题讨论】:

    标签: sqlite join xamarin xamarin.forms


    【解决方案1】:

    我想你可以看看sqlite-net-extensions

    SQLite-Net Extensions 是一个非常简单的 ORM,它在 sqlite 之上提供了一对一、一对多、多对一、多对多、反向和文本块状关系-网络图书馆。 sqlite-net 是一个开源的最小库,允许 .NET 和 Mono 应用程序将数据存储在 SQLite 3 数据库中。 SQLite-Net Extensions 扩展了它的功能以帮助用户处理 sqlite-net 实体之间的关系。

    public class Stock
        {
            [PrimaryKey, AutoIncrement]
            public int Id { get; set; }
            [MaxLength(8)]
            public string Symbol { get; set; }
    
            [OneToMany(CascadeOperations = CascadeOperation.All)]      // One to many relationship with Valuation
            public List<Valuation> Valuations { get; set; }
        }
    
    
            public class Valuation
            {
                [PrimaryKey, AutoIncrement]
                public int Id { get; set; }
    
                [ForeignKey(typeof(Stock))]     // Specify the foreign key
                public int StockId { get; set; }
                public DateTime Time { get; set; }
                public decimal Price { get; set; }
    
                [ManyToOne]      // Many to one relationship with Stock
                public Stock Stock { get; set; }
            }
    

    以下是我们将如何创建、读取和更新实体:

    var db = Utils.CreateConnection();
    db.CreateTable<Stock>();
    db.CreateTable<Valuation>();
    
    var euro = new Stock() {
        Symbol = "€"
    };
    db.Insert(euro);   // Insert the object in the database
    
    var valuation = new Valuation() {
        Price = 15,
        Time = DateTime.Now,
    };
    db.Insert(valuation);   // Insert the object in the database
    
    // Objects created, let's stablish the relationship
    euro.Valuations = new List<Valuation> { valuation };
    
    db.UpdateWithChildren(euro);   // Update the changes into the database
    if (valuation.Stock == euro) {
        Debug.WriteLine("Inverse relationship already set, yay!");
    }
    
    // Get the object and the relationships
    var storedValuation = db.GetWithChildren<Valuation>(valuation.Id);
    if (euro.Symbol.Equals(storedValuation.Stock.Symbol)) {
        Debug.WriteLine("Object and relationships loaded correctly!");
    }
    

    【讨论】:

      猜你喜欢
      • 2019-10-15
      • 1970-01-01
      • 1970-01-01
      • 2012-09-03
      • 2020-10-22
      • 2017-05-19
      • 2020-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多