Reputation: 417
I am dealing with SQLite in windows store application.I am updating the values in table using
var tagPage = db.QueryAsync<MyModel>("UPDATE MyModel SET Tag =2 WHERE id = 1");
db.UpdateAsync(tagPage);
It throws an NotSupportedException on SQLite.cs class over the method
public int Update(object obj, Type objType)
{
if (obj == null || objType == null)
{
return 0;
}
var map = GetMapping(objType);
var pk = map.PK;
if (pk == null)
{
throw new NotSupportedException("Cannot update " + map.TableName + ": it has no PK");
}
var cols = from p in map.Columns
where p != pk
select p;
var vals = from c in cols
select c.GetValue(obj);
var ps = new List<object>(vals);
ps.Add(pk.GetValue(obj));
var q = string.Format("update \"{0}\" set {1} where {2} = ? ", map.TableName, string.Join(",", (from c in cols
select "\"" + c.Name + "\" = ? ").ToArray()), pk.Name);
return Execute(q, ps.ToArray());
}
because I think it wont get the primery key, where I have provided primery key in table. I tried it with async and await but no use, why it is happening? please help me
Upvotes: 2
Views: 1017
Reputation: 18823
Regarding your NotSupportedException
issue - I suspect that your model is missing the PrimaryKey
attribute:
public class MyModel
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public int Tag { get; set; }
public string Foo { get; set; }
}
You will need the attribute(s) above, even if your db schema already has them defined. At this time, Sqlite-Net does not read schema data from the db.
Some other thoughts:
Firstly, you should use only either the synchronous Sqlite-Net API, or the async API. Don't use both.
I personally prefer the synchronous API, as it appears to be more up to date. The async API has not been updated in 11 months, whereas the synchronous API was updated 4 months ago. Besides this I never was able to figure out how to do transactions using the async API. Again, this is personal preference.
Secondly, the Query
and QueryAsync
methods should be used only for querying (for SELECT
s). They should not be used for UPDATE
s. For adding/changing data, you will want to use these synchronous methods: Execute
, Update
, Insert
, etc. If using the async API, there are the async counterparts (ExecuteAsync
, etc).
Please read the Sqlite-Net Project page, as you will find a lot of helpful information there regarding general API usage.
Upvotes: 5