0

I have a remote web service that returns me two int values namely:

  1. Frequency
  2. Conserve

I have a model defined as Parameters that is as follows :

public class Parameters
{
    public int Frequency { get; set; }
    public int Conserve { get; set; }
}

On each start of the application, a call is made to the web service to get the values. At the first launch of the app, the values are saved in to an sqlite database.

Subsequently, after each start of the application, a call is made to the web service to get the values. Each and every time the values are saved to the sqlite db.

The method that does is:

public async Task InsertParams()
{
        await con.CreateTableAsync<Parameters>();

        ParamService ps = new ParamService();

        Parameters pm = new Parameters();

        pm = await ps.GetParams(); // call to webservice

        await con.InsertOrReplaceAsync(pm);
}

The issue is that if initially the value of frequency is 30 on the web service, it inserts it in the db. But when the value changes to 50, it does not override the row in the sqlite db.

I want only a single row in the sqlite db that should be overriden /replace. Please advise how can I achieve this. Thanks.

crazydev
  • 575
  • 2
  • 9
  • 30
  • How does it know which row to update? – Josh Adams Jan 08 '18 at 12:35
  • Possible duplicate of [How to use InsertOrReplace in sqlite.net PCL?](https://stackoverflow.com/questions/25409870/how-to-use-insertorreplace-in-sqlite-net-pcl) – EvZ Jan 08 '18 at 13:05

1 Answers1

0

InsertOrReplace method requires a unique identifier ID.
Otherwise how do you expect it to check if a row exists and update it if needed?

So you have to options:
1. Add an Id property and keep it the same all the time.

public class Parameters
{
    [PrimaryKey]
    public int Id { get; set; }
    public int Frequency { get; set; }
    public int Conserve { get; set; }
}

...
// TODO: Hacky solution
pm = await ps.GetParams(); // call to webservice
pm.Id = 1;
await con.InsertOrReplaceAsync(pm);
...
  1. Instead of InsertOrReplace truncate the table and insert a single row.

P.S.: Keeping a single row in a SQL table does not feel natural, you may consider some other caching solutions in order to valid use of hacks described above.

EvZ
  • 11,889
  • 4
  • 38
  • 76
  • Where can I add the Id property and how can I keep it the same all the time ? – crazydev Jan 08 '18 at 12:38
  • I updated my answer. Hope you get a better idea now. – EvZ Jan 08 '18 at 12:41
  • But the webservice returns a value of 0 for the Id property. – crazydev Jan 08 '18 at 12:41
  • Sure it does. Since the web response does not contains Id, the deserializer set the Id property value to default(int) which is 0. – EvZ Jan 08 '18 at 12:43
  • One last question. How can I retrieve only the field Frequency in sqlite ? var result = await _con.ExecuteAsync("SELECT Frequency from Parameters WHERE Id = ?", 1); return result.Frequency; its not working. – crazydev Jan 08 '18 at 13:01
  • Thats another question. If you can't find the answer on stackoverflow or any other web resource then open a new thread and I will be glad to help. – EvZ Jan 08 '18 at 13:03