【问题标题】:How to format date and time values for SQLite?如何格式化 SQLite 的日期和时间值?
【发布时间】:2011-05-10 17:52:17
【问题描述】:

不敢相信我必须在这里问这个。甚至谷歌搜索也无济于事。

我想这样做:

insert into atable (adatefield,atimefield,adatetimefield) values (A,B,C);
  • adatefield 定义为 DATE
  • atimefield 被定义为 TIME
  • 日期时间字段定义为日期时间

A、B 和 C 应该放什么?

它是免费的,但 SQLite 的文档很糟糕。

【问题讨论】:

    标签: sqlite datetime date time format


    【解决方案1】:

    日期、时间和日期时间字段实际上都将存储日期时间,这取决于您插入其中的内容,并且它们都适用于日期字符串。

    你可以

    insert into atable values("2010-11-16","14:12:22","2010-11-16 14:12:22");
    

    或者您实际上可以在所有字段中插入“2010-11-16 14:12:22”,然后选择它们:

    select date(adatefield), time(atimefield), datetime(adatetimefield) from atable;
    

    但是,如果您想确保字段仅包含日期、时间或日期时间,并且您从变量中插入,那么您可以

    insert into atable values(date(thedate),time(thedate),datetime(thedate));
    

    Sqlites 无类型使某些事情变得非常简单,但可能会使其他事情变得混乱或复杂。

    【讨论】:

      猜你喜欢
      • 2018-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多