1

so I've been trying a bit around sqlite-net-pcl and this just doesn't seem to be able to update the status... After trying around a bit with a teststring to somewhere localize the problem it starts with the first list which appears to be filled with 0's only so I've somewhat reverted it back to where it was.

public async Task UpdateStatus()
    {
        var ObjectIDList = await database.QueryAsync<long>("SELECT ObjectID FROM Object WHERE ObjectStatus = 0 OR ObjectStatus = 1");
        if (ObjectIDList != null)
        {
            foreach (long ObjectID in ObjectIDList)
            {
                byte newStatus = 5;
                var result = await database.Table<Object>().Where(i => i.ObjectID == ObjectID).FirstOrDefaultAsync();
                if (result != null)
                {
                    result.Objectstatus = newStatus;
                    await SaveObjectAsync(result);
                }
            }
        }
    }

No matter how many entries there are in my table, whenever there is either a 0 or a 1 in the object's status value it filled the list with another 0.

Question

Why is ObjectIDList always returning zeros even though I have many records in the database whose ObjectStatus = 0 or ObjectStatus = 1? If I have 5 records, then it returns 5 results but with zeros instead of the actual ObjectID.

4

1 回答 1

1

我查看了源代码,这就是 SQLite 的作用。假设您有这样的查询:

var ObjectIDList = await database.QueryAsync<long>("SELECT ObjectID FROM 
    Object WHERE ObjectStatus = 0 OR ObjectStatus = 1");

SQLite 将对数据库执行查询,并创建一个T传递给QueryAsync<T>. 你通过了 along所以它会创建一个新的long. 然后它将尝试填充ObjectID实例中命名的属性。但显然,由于long没有名为 的属性ObjectID,因此无法填充它。SQLite 不会抛出异常来告诉你这一点。它只是继续,最后你会得到一堆新创建long的 s。默认情况下,所有longs 都设置为零。这就是为什么你在ObjectIDList.

查看从第 119 行开始的源代码。就个人而言,我认为这是一个糟糕的设计,long在这种情况下他们应该只返回一个 s 列表。

使固定

所以要解决这个问题,您要么需要创建一个具有ObjectID属性的新类,要么使用您已经拥有的类:(Object这不是 .net Object 类型,而是您自己的自定义类型)。所以您的查询将变为:

var ObjectIDList = await database.QueryAsync<Object>("SELECT ObjectID FROM 
    Object WHERE ObjectStatus = 0 OR ObjectStatus = 1");

然后这样做:

List<long> ids = ObjectIDList.Select(x => x.ObjectID).ToList();

现在循环并做你的工作:

foreach(long thisObjectId in ids)
{
    // code...
}
于 2018-02-01T00:47:58.070 回答