问题描述
如何使用此函数通过获取每一行的 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);
}