将朱利安日期列添加到 SQLite 表

发布于 2024-10-09 14:56:05 字数 239 浏览 7 评论 0原文

这很简单,但我无法从 SQLite FAQ 和 ALTER TABLE 的小插图中弄清楚。

我有一个日期列,但为了找到时差,我需要使用儒略日期,所以我觉得我不妨添加一个儒略日期列,因为我会经常这样做。但我不知道该怎么做。

我已经经历了很多次迭代,但我认为我并没有理解 ALTER TABLEINSERT INTO 命令的 SQLite 哲学。谢谢!

This is easy, but I can't figure it out from the SQLite FAQ and vignette for ALTER TABLE.

I have a date column, but to find a time difference, I need to use julian dates, so I feel like I might as well add a julian date column because I'll be doing this a lot. But I can't figure out how to do it.

I've gone through a bunch of iterations, but I don't think I'm clicking with the SQLite philosophy of the ALTER TABLE and INSERT INTO commands. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红玫瑰 2024-10-16 14:56:05

但我不知道如何永久
向表中添加一列
其他列的功能
桌子。这不是常见用法吗?

您无法在 SQLite 表中添加计算列。但是您可以使用 VIEW 来做到这一点。

CREATE TABLE TableWithDates (
    ID INTEGER PRIMARY KEY,
    Iso8601 TEXT
);

CREATE VIEW ViewWithDates AS
    SELECT ID, Iso8601, JULIANDAY(Iso8601) AS Julian
    FROM TableWithDates;

sqlite> INSERT INTO TableWithDates VALUES (1, CURRENT_TIMESTAMP);
sqlite> SELECT * FROM TableWithDates;
1|2010-12-23 21:13:26
sqlite> SELECT * FROM ViewWithDates;
1|2010-12-23 21:13:26|2455554.3843287

But I have no idea how to permanently
add a column to a table that is a
function of other columns in that
table. Is this not common usage?

You can't add a calculated column in an SQLite table. But you can do that with a VIEW.

CREATE TABLE TableWithDates (
    ID INTEGER PRIMARY KEY,
    Iso8601 TEXT
);

CREATE VIEW ViewWithDates AS
    SELECT ID, Iso8601, JULIANDAY(Iso8601) AS Julian
    FROM TableWithDates;

sqlite> INSERT INTO TableWithDates VALUES (1, CURRENT_TIMESTAMP);
sqlite> SELECT * FROM TableWithDates;
1|2010-12-23 21:13:26
sqlite> SELECT * FROM ViewWithDates;
1|2010-12-23 21:13:26|2455554.3843287
夏见 2024-10-16 14:56:05

Sqlite 中没有 DATE 或 TIME 类型。您可以自由选择对您有意义的内容。如果只是用于记录的时间戳或其他内容,则 STRING 应该没问题。如果您要做日期时间比较和数学(根据问题),那么 INTEGER 是最好的。

此页面讨论内置函数的使用。如果使用 INTEGER 存储自纪元以来的秒数,则可以使用减法来查找时间差。

There are no DATE or TIME types in Sqlite. You're free to select what makes sense for you. If it's just timestamps for logging or something, STRING should be ok. If you're going to do datetime comparisons and math (which you are according to the question), INTEGER would be best.

This page discuses built-in functions for use. If you use INTEGER to store seconds since epoch, you can use subtraction to find a time difference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文