微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何通过获取每一行的 id 来更新数据库中使用此函数的每一行?

如何解决如何通过获取每一行的 id 来更新数据库中使用此函数的每一行?

如何使用此函数通过获取每一行的 id 来更新 sqlite-net-pcl 数据库中的每一行,即使我稍后创建了新行。为了分解它,我需要表中每个现有行的 id,我需要将每个 id 放入此函数并更新表。

private void refreshData()
{
    using (sqliteConnection conn = new sqliteConnection(App.FilePath))
    {
        var **buyAmount** = conn.Get<userInput>(**id**).buyAmount;
        var **symbol** = conn.Get<userInput>(**id**).Pair.ToString();

        HttpClient client = new HttpClient();
        var response = await client.GetStringAsync("https://api.binance.com/api/v3/ticker/price?symbol=" + **symbol**);
        var cryptoconverted = JsonConvert.DeserializeObject<Crypto>(response);
        var currentPriceDouble = double.Parse(cryptoconverted.price);
        var finalAnswer = double.Parse(cryptoconverted.price) * double.Parse(**buyAmount**);
        conn.Execute("UPDATE userInput SET worth = " + finalAnswer + " where Id= " + **id**);
    };
}

userInput.cs

using System;
using sqlite;

namespace CryptoProject.Classes
{
    public class userInput
    {
        [PrimaryKey,AutoIncrement]
        public int Id { get; set; }
        public string Pair { get; set; }
        public string buyPrice { get; set; }
        public string buyAmount { get; set; }
        public double spent{ get; set; }
        public double worth{ get; set; }

        public userinput()
        {
        }

        public static implicit operator string(userInput v)
        {
            throw new NotImplementedException();
        }
    }
}


解决方法

如果要遍历表中的每一行并更新它,请查询所有行的列表并使用 foreach 遍历它们

// get all rows from your db
var stocks = conn.Table<userInput>().ToList();

// loop through each row
forech(var s in stocks)
{
  // do whatever logic you need here
  ...

  // then update the object properties
  s.worth = ...
  ...

  // then save
  conn.Update(s);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。