How to update every row using this function in database by getting the id of every row?

127 views Asked by At

How do I update every row in sqlite-net-pcl database using this function by getting the id of every row even though i create new rows later on. To break it down, I need the id of every existing row in my table, and i need each one of that id to be put in this function and update the table.

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();
        }
    }
}


1

There are 1 answers

4
Jason On BEST ANSWER

if you want to loop through every row in your table and update it, then query a list of all rows and iterate through them with 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);
}