【问题标题】:DateTime is not correct when querying from sqlite从 sqlite 查询时 DateTime 不正确
【发布时间】:2019-10-07 17:37:59
【问题描述】:

我使用 AzureMobileServices 同步数据,但使用 SQLite 检索数据,因为我无法在 azure 服务中执行原始查询。

我能够获取记录,但我的问题是表中的 DateTime 列。 我总是收到奇怪的日期,例如 01/01/0001 或类似的日期。

我尝试过的事情:

Parse<Model>().Query("select datetime(CreatedAt, 'unixepoch') CreatedAt from table"); 

Parse<Model>().Query("select strftime('%d/%m/%Y',CreatedAt / 10000000 - 62135596800,'unixepoch')   as createdAt from table");

只有当我运行执行标量并将其解析为这样的字符串时,我才能获得正确的日期和时间:

connection.ExecuteScalar<string>("select datetime(CreatedAt, 'unixepoch') as createdAt from table limit 1");

它没有被正确地解析到我的具有这个属性的模型中

public DateTime? CreatedAt {get; set;}

编辑

这就是它在 sqlite 中的样子

有什么建议吗?

【问题讨论】:

  • 您可能必须解析为 DateTime 并转换为具有您模型的正确格式的字符串。日期如何存储?采用哪种格式?
  • 我之前没有使用过 AzureMobileServices,经过一番搜索,可能是因为您的 ? 属性为空。请尝试将其更改为 Nullable&lt;DateTime&gt;。看到这个帖子:stackoverflow.com/questions/16692647/…
  • 我不明白的是这是有效的:ExecuteScalar&lt;string&gt;("select strftime('%m/%d/%Y %H:%M:%S',datetime(CreatedAt,'unixepoch')) from tblRegister limit 1") 而 ExecuteScalar(相同的查询)不起作用
  • @RobScott 我尝试了 Nullable 但问题仍然存在。
  • @AwaisShaikh 在这种情况下,你的Parse&lt;Model&gt;().Query(....%d/%m/%Y,而不是%m/%d/%Y,正如你上面评论的那样

标签: c# sqlite xamarin xamarin.forms azure-mobile-services


【解决方案1】:

DateTime 默认值为 01/01/0001。检查您用于创建模型的方法。由于不兼容,CreatedAt 属性会引发默认值。

【讨论】:

    猜你喜欢
    • 2021-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-21
    • 2014-07-01
    • 2013-08-01
    • 1970-01-01
    • 2023-04-10
    相关资源
    最近更新 更多