如何将 SQLite 中的当前时间戳存储为刻度?
我有一个 SQLite 数据库,我将日期存储为刻度。我没有使用默认的 ISO8601 格式。假设我有一个定义如下的表:
CREATE TABLE TestDate (LastModifiedTime DATETIME)
我希望使用 SQL 插入当前日期和时间。如果我执行以下任何语句,我最终都会将日期和时间存储为字符串而不是刻度。
INSERT INTO TestDate (LastModifiedTime) VALUES(CURRENT_TIMESTAMP)
INSERT INTO TestDate (LastModifiedTime) VALUES(DateTime('now'))
我查看了 SQLite 文档,但我似乎没有找到任何选项来获取当前时间戳(以刻度为单位)。
我当然可以在 C# 中定义一个参数并将该值存储为 System.DateTime。这确实会导致日期时间以刻度为单位存储到数据库中。
我想做的是能够直接从 SQL 语句中插入和更新当前时间戳。我该怎么做?
编辑:
我希望将数据作为刻度存储在数据库中的原因是,日期的存储格式与 ADO.Net 数据提供程序存储的格式相同,因此当数据也使用 ADO.Net 提供程序进行查询时,它会作为 System.DataTime .Net 类型正确检索。
I have a SQLite database where I store the dates as ticks. I am not using the default ISO8601 format. Let's say I have a table defined as follows:
CREATE TABLE TestDate (LastModifiedTime DATETIME)
Using SQL, I wish to insert the current date and time. If I execute any of the below statements, I end up getting the date and time stored as a string and not in ticks.
INSERT INTO TestDate (LastModifiedTime) VALUES(CURRENT_TIMESTAMP)
INSERT INTO TestDate (LastModifiedTime) VALUES(DateTime('now'))
I have looked at the SQLite documenation, but I do not seem to find any option to obtain the current timestamp in ticks.
I can of course define a parameter in C# and store the value as a System.DateTime. This does result in the datetime getting stored to the database in ticks.
What I would like to do is be able to insert and update the current timestamp directly from within the SQL statement. How would I do this?
Edit:
The reason I want the data stored as ticks in the database, is that the dates are stored in the same format as stored by the ADO.Net data provider, and so that when the data is also queried using the ADO.Net provider it is correctly retrieved as a System.DataTime .Net type.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
SQLite 的这一特殊之处给我带来了很大的痛苦。
简单的方法 - 作为常规时间戳存储和检索
输出: 2011-05-10 21:34:46|1305063286.46.000
痛苦的方法 - 作为 UNIX 时间戳存储和检索
您可以使用 strftime 检索刻度值。此外,要存储 UNIX 时间戳(大致相当于刻度),您可以将秒数括在单引号中。
SQLite 会在内部将其存储为非 UNIX 时间戳的其他值。在检索时,您需要显式告诉 SQLite 将其作为 UNIX 时间戳进行检索。
要存储当前日期和时间,请使用 strftime('%s', 'now')。
完整示例:
当由 sqlite3 执行时,此脚本带有打印:
This particular oddity of SQLite caused me much anguish.
Easy way - store and retrieve as regular timestamp
Output: 2011-05-10 21:34:46|1305063286.46.000
Painful way - store and retrieve as a UNIX timestamp
You can use strftime to retrieve the value in ticks. Additionally, to store a UNIX timestamp (roughly equivalent to ticks), you can can surround the number of seconds in single-quotes.
SQLite will store this internally as some other value that is not a UNIX timestamp. On retrieval, you need to explicitly tell SQLite to retrieve it as a UNIX timestamp.
To store the current date and time, use strftime('%s', 'now').
Full example:
When executed by sqlite3, this script with print:
经过进一步研究 SQLite 文档 和其他有关日期数字转换的信息,我想出了下面的公式,它似乎产生了正确的结果:
似乎是一种痛苦的方式来产生我希望可以作为内置日期时间格式使用的东西,特别是数据库支持以刻度形式存储日期时间值。希望这对其他人也有用。
更新:
对于夏令时,上述公式并不完美。请参阅 SQLite 文档中有关本地时间计算的注意事项和错误部分。
After further study of the SQLite documentation and other information found on date number conversions, I have come up with the following formula, which appears to produce correct results:
Seems like a painful way to produce something that I would expect to be available as a built-in datetime format, especially that the database supports the storing of datetime values in ticks. Hopefully, this becomes useful for others too.
Update:
The above formula is not perfect when it comes to daylight savings. See section Caveats And Bugs in SQLite docs regarding local time calculation.
以下将返回自 UNIX 纪元以来的毫秒数:
它的工作原理是获取自 UNIX 纪元 (%s) 以来的秒数,减去当前时间的秒数 ( %S),加上带小数位的秒数 (%f),并将结果乘以 1000,将秒转换为毫秒。
减法和加法是为了增加值的精度而不扭曲结果。正如 SQLite 文档中所述,同一步骤中所有使用 'now' 将返回相同的值。
The following will return the number of milliseconds since the UNIX Epoch:
It works by grabbing the number of seconds since the Unix Epoch (%s), subtracting the number of seconds in the current time (%S), adding the number of seconds with decimal places (%f), and multiplying the result by 1000 to convert from seconds to milliseconds.
The subtraction and addition are to add precision to the value without skewing the result. As stated in the SQLite Documentation, all uses of 'now' within the same step will return the same value.