DATETIME 值如何在 SQLite 中工作?

2021-12-08 00:00:00 datetime sqlite

我正在创建 Android 应用,需要保存创建记录的日期/时间.然而,SQLite 文档说,SQLite 没有专门用于存储日期和/或时间的存储类",并且它能够将日期和时间存储为 TEXT、REAL 或 INTEGER 值".

I’m creating Android apps and need to save date/time of the creation record. The SQLite docs say, however, "SQLite does not have a storage class set aside for storing dates and/or times" and it's "capable of storing dates and times as TEXT, REAL, or INTEGER values".

使用一种类型而不是另一种类型是否有技术原因?单列是否可以以三种格式中的任何一种从行到行存储日期?

Is there a technical reason to use one type over another? And can a single column store dates in any of the three formats from row to row?

我稍后需要比较日期.例如,在我的应用中,我将显示在日期 A 到日期 B 之间创建的所有记录.我担心没有真正的 DATETIME 列可能会使比较变得困难.

I will need to compare dates later. For instance, in my apps I will show all records that are created between date A until date B. I am worried that not having a true DATETIME column could make comparisons difficult.

推荐答案

SQlite 没有特定的日期时间类型.您可以使用 TEXTREALINTEGER 类型,以适合您的需要.

SQlite does not have a specific datetime type. You can use TEXT, REAL or INTEGER types, whichever suits your needs.

SQLite 没有专门用于存储日期和/或时间的存储类.相反,SQLite 的内置日期和时间函数能够将日期和时间存储为 TEXT、REAL 或 INTEGER 值:

SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:

  • TEXT 作为 ISO8601 字符串(YYYY-MM-DD HH:MM:SS.SSS").
  • REAL 作为儒略日数,即公元前 4714 年 11 月 24 日格林威治中午以来的天数.根据预兆公历.
  • INTEGER 作为 Unix 时间,自 1970-01-01 00:00:00 UTC 以来的秒数.

应用程序可以选择以任何这些格式存储日期和时间,并使用内置的日期和时间函数在格式之间自由转换.

Applications can chose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.

SQLite 内置日期和时间函数可以在这里找到.

SQLite built-in Date and Time functions can be found here.

相关文章